diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2023-12-21 16:21:02 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2023-12-21 16:21:02 +0200 |
commit | b73091a7f7706e55b75e34263814a47ba7d0c8f8 (patch) | |
tree | d77eb8316f8e6ecc4705ea495544a52e9ac33cf6 | |
parent | 81226dcaf39cfba29704645aeb5cd6de55da67f0 (diff) | |
download | epq-web-b73091a7f7706e55b75e34263814a47ba7d0c8f8.tar.gz epq-web-b73091a7f7706e55b75e34263814a47ba7d0c8f8.tar.bz2 epq-web-b73091a7f7706e55b75e34263814a47ba7d0c8f8.zip |
FATAL FIXME No underlying STOMP connection
-rw-r--r-- | src/App.tsx | 4 | ||||
-rw-r--r-- | src/Chat/Chat.tsx | 101 | ||||
-rw-r--r-- | src/Chat/ChatMessage.tsx | 34 | ||||
-rw-r--r-- | src/Chat/types.tsx | 5 |
4 files changed, 74 insertions, 70 deletions
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 ( <div className="App"> <h1>Local Area Network Chat Application</h1> <pre>This web application was built for the purposes of an EPQ project.</pre> - <Chat user="testuser" /> + <ChatWrapper user="testuser" brokerURL=""/> </div> ) } 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<string>("/app/chat") + const [connected, setConnected] = useState(false) + const [subscribe, setSubscribe] = useState("/sub/chat") + const [username, setUsername] = useState<string>(user) + const [children, setChildren] = useState<React.ReactElement[]>([]) + stompClient.onConnect = (frame) => { + setConnected(true); + stompClient.subscribe(subscribe, (message) => { + const {from, to, content} = JSON.parse(message.body) as MessageType + const messageElement = <Message sender={from} text={content} /> + 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(<Message sender={data.from} text={data.message} />) - // 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(<Message sender={data.from} text={data.message} />) + // // TODO Create new message + // // DDL 20 DEC + // } + // }) return ( <div className="chat"> - <div className={msgWrapperClassName}> - {messageElementsArray} + <button onClick={ev => connect()}>Connect</button> + <button onClick={ev => disconnect()}>Disconnect</button> + <div className="chat-inner"> + {children} </div> <span><input id="data-entry"></input><button onClick={ev => sendDataButtonHandler(ev)}>Send</button></span> </div> ) } -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<string>, - toIPs: Array<string>, - 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 |