From 49e7eacb0bcc03805d0b85bfa44cca08f8984de1 Mon Sep 17 00:00:00 2001 From: Zhongheng Liu Date: Thu, 21 Dec 2023 12:34:29 +0200 Subject: [PATCH] added type compatibility layer added text data entry for sending new messages added websockets connection added type annots UNTESTED interop between TSX and Java backend --- src/App.tsx | 2 ++ src/Chat/Chat.tsx | 42 ++++++++++++++++++++++++++++++++-------- src/Chat/ChatMessage.tsx | 11 +++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index ecf2d19..fa2cdc8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,9 +1,11 @@ import React from "react"; +import Chat from "./Chat/Chat"; const App = (): React.ReactElement => { return (

Local Area Network Chat Application

This web application was built for the purposes of an EPQ project.
+
) } diff --git a/src/Chat/Chat.tsx b/src/Chat/Chat.tsx index be57614..50e5a0b 100644 --- a/src/Chat/Chat.tsx +++ b/src/Chat/Chat.tsx @@ -1,4 +1,6 @@ import React from "react"; +import { Attachment, Avatar, ChatMessage, ServerMsgType, User } from "./ChatMessage"; +import { connect } from "http2"; const domain = "localhost" const port = "8080" const connectionAddress = `ws://${domain}:${port}` @@ -6,20 +8,37 @@ const Message = ( { sender, text, - objects, }: { - sender: string, // TODO Create specific sender object type + sender: string text: string, - objects?: Array, } - ): React.ReactElement => { + ): React.ReactElement<{sender: string, text: string}> => { - return (<>) + return (

Message from {sender}: {text}

) } -const Chat = (): React.ReactElement => { +const Chat = ( + { + user + }: + { + user: string, + } +): React.ReactElement => { + var messageElementsArray: JSX.Element[] = []; const msgWrapperClassName = "msg-wrapper" const connection = new WebSocket(connectionAddress) + const sendDataButtonHandler = (ev: React.MouseEvent) => { + ev.preventDefault() + const entryElement: HTMLInputElement = document.getElementById("data-entry") as HTMLInputElement + const messageData = + { + from: user, + to: "everyone", + content: entryElement.value + } + connection.send(JSON.stringify(messageData)) + } connection.addEventListener("open", (ev: Event) => { ev.preventDefault() connection.send("Hello world!") @@ -27,20 +46,27 @@ const Chat = (): React.ReactElement => { connection.addEventListener("message", (ev: MessageEvent) => { ev.preventDefault() const wrappers = document.getElementsByClassName(msgWrapperClassName) - const data = JSON.parse(ev.data) + + // Matches data from JSON data input against ChatMessage datatype for processing + const data = JSON.parse(ev.data) as ChatMessage for (let index = 0; index < wrappers.length; index++) { const element: Element | null = wrappers.item(index); if (!element) { console.error("msgWrapper class cannot be found! Message not delivered.") return } + messageElementsArray.push() + // TODO Create new message + // DDL 20 DEC } }) return (
+ {messageElementsArray}
- +
) } +export default Chat; \ No newline at end of file diff --git a/src/Chat/ChatMessage.tsx b/src/Chat/ChatMessage.tsx index 5d8069c..f9ae9e4 100644 --- a/src/Chat/ChatMessage.tsx +++ b/src/Chat/ChatMessage.tsx @@ -6,6 +6,14 @@ enum FileType { EXEC_BINARY, UNKNOWN, } +export type User = { + name: string, + userId: string, + avatar: Avatar +} +export type Avatar = { + pictureUri: string, +} export type Attachment = { name: string, uri: string, @@ -21,3 +29,6 @@ export type ChatMessage = { message: string, attachments: Attachment } +export type ServerMsgType = { + +}