From b73091a7f7706e55b75e34263814a47ba7d0c8f8 Mon Sep 17 00:00:00 2001 From: Zhongheng Liu Date: Thu, 21 Dec 2023 16:21:02 +0200 Subject: [PATCH] FATAL FIXME No underlying STOMP connection --- src/App.tsx | 4 +- src/Chat/Chat.tsx | 101 ++++++++++++++++++++++++++------------- src/Chat/ChatMessage.tsx | 34 ------------- src/Chat/types.tsx | 5 ++ 4 files changed, 74 insertions(+), 70 deletions(-) delete mode 100644 src/Chat/ChatMessage.tsx create mode 100644 src/Chat/types.tsx diff --git a/src/App.tsx b/src/App.tsx index fa2cdc8..75a5685 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,11 +1,11 @@ import React from "react"; -import Chat from "./Chat/Chat"; +import ChatWrapper 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 288654e..0956858 100644 --- a/src/Chat/Chat.tsx +++ b/src/Chat/Chat.tsx @@ -1,63 +1,96 @@ -import React from "react"; -import { Attachment, Avatar, ChatMessage, ServerMsgType, User } from "./ChatMessage"; -import { connect } from "http2"; +import React, { useState } from "react"; import { Message } from "./Message"; import { Client, Stomp } from "@stomp/stompjs"; +import { MessageType } from "./types"; const domain = "localhost" const port = "8080" const connectionAddress = `ws://${domain}:${port}/ws` -const stompClient = new Client() -const Chat = ( +const ChatWrapper = ( { - user + user, + brokerURL, }: { user: string, + brokerURL: string, } ): React.ReactElement => { - var messageElementsArray: JSX.Element[] = []; - const msgWrapperClassName = "msg-wrapper" - const connection = new WebSocket(connectionAddress) + const stompClient = new Client({ + brokerURL: connectionAddress + }) + const [destination, setDestination] = useState("/app/chat") + const [connected, setConnected] = useState(false) + const [subscribe, setSubscribe] = useState("/sub/chat") + const [username, setUsername] = useState(user) + const [children, setChildren] = useState([]) + stompClient.onConnect = (frame) => { + setConnected(true); + stompClient.subscribe(subscribe, (message) => { + const {from, to, content} = JSON.parse(message.body) as MessageType + const messageElement = + children.push(messageElement) + }) + } + stompClient.onWebSocketError = (error) => { + console.error('Error with websocket', error); + }; + + stompClient.onStompError = (frame) => { + console.error('Broker reported error: ' + frame.headers['message']); + console.error('Additional details: ' + frame.body); + }; + const sendDataButtonHandler = (ev: React.MouseEvent) => { console.log("WebSockets handler invoked.") ev.preventDefault() const entryElement: HTMLInputElement = document.getElementById("data-entry") as HTMLInputElement const messageData = { - from: user, + from: username, to: "everyone", content: entryElement.value } - connection.send(JSON.stringify(messageData)) + stompClient.publish({ + body: JSON.stringify(messageData), + destination: destination + }) } - connection.addEventListener("open", (ev: Event) => { - ev.preventDefault() - connection.send("Hello world!") - }) - connection.addEventListener("message", (ev: MessageEvent) => { - ev.preventDefault() - const wrappers = document.getElementsByClassName(msgWrapperClassName) + const connect = () => { + stompClient.activate() + } + const disconnect = () => { + stompClient.deactivate() + } + // connection.addEventListener("open", (ev: Event) => { + // ev.preventDefault() + // connection.send("Hello world!") + // }) + // connection.addEventListener("message", (ev: MessageEvent) => { + // ev.preventDefault() + // const wrappers = document.getElementsByClassName(msgWrapperClassName) - // 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 - } - }) + // // 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} + + +
+ {children}
) } -export default Chat; \ No newline at end of file +export default ChatWrapper; \ No newline at end of file diff --git a/src/Chat/ChatMessage.tsx b/src/Chat/ChatMessage.tsx deleted file mode 100644 index f9ae9e4..0000000 --- a/src/Chat/ChatMessage.tsx +++ /dev/null @@ -1,34 +0,0 @@ -enum FileType { - FILE_TXT, - DOCUMENT_WORD, - DOCUMENT_PDF, - IMAGE, - EXEC_BINARY, - UNKNOWN, -} -export type User = { - name: string, - userId: string, - avatar: Avatar -} -export type Avatar = { - pictureUri: string, -} -export type Attachment = { - name: string, - uri: string, - sizeBytes: number, - filetype: FileType -} -export type ChatMessage = { - from: string, - fromIP: string, - to: Array, - toIPs: Array, - timestampPosted: number, - message: string, - attachments: Attachment -} -export type ServerMsgType = { - -} diff --git a/src/Chat/types.tsx b/src/Chat/types.tsx new file mode 100644 index 0000000..5ab5af7 --- /dev/null +++ b/src/Chat/types.tsx @@ -0,0 +1,5 @@ +export type MessageType = { + from: string, + to: string, + content: string +} \ No newline at end of file