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
This commit is contained in:
Zhongheng Liu 2023-12-21 12:34:29 +02:00
commit 49e7eacb0b
No known key found for this signature in database
3 changed files with 47 additions and 8 deletions

View file

@ -1,9 +1,11 @@
import React from "react"; import React from "react";
import Chat from "./Chat/Chat";
const App = (): React.ReactElement => { const App = (): React.ReactElement => {
return ( return (
<div className="App"> <div className="App">
<h1>Local Area Network Chat Application</h1> <h1>Local Area Network Chat Application</h1>
<pre>This web application was built for the purposes of an EPQ project.</pre> <pre>This web application was built for the purposes of an EPQ project.</pre>
<Chat user="testuser" />
</div> </div>
) )
} }

View file

@ -1,4 +1,6 @@
import React from "react"; import React from "react";
import { Attachment, Avatar, ChatMessage, ServerMsgType, User } from "./ChatMessage";
import { connect } from "http2";
const domain = "localhost" const domain = "localhost"
const port = "8080" const port = "8080"
const connectionAddress = `ws://${domain}:${port}` const connectionAddress = `ws://${domain}:${port}`
@ -6,20 +8,37 @@ const Message = (
{ {
sender, sender,
text, text,
objects,
}: }:
{ {
sender: string, // TODO Create specific sender object type sender: string
text: string, text: string,
objects?: Array<any>,
} }
): React.ReactElement => { ): React.ReactElement<{sender: string, text: string}> => {
return (<></>) return (<p>Message from {sender}: {text}</p>)
} }
const Chat = (): React.ReactElement => { const Chat = (
{
user
}:
{
user: string,
}
): React.ReactElement => {
var messageElementsArray: JSX.Element[] = [];
const msgWrapperClassName = "msg-wrapper" const msgWrapperClassName = "msg-wrapper"
const connection = new WebSocket(connectionAddress) 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) => { connection.addEventListener("open", (ev: Event) => {
ev.preventDefault() ev.preventDefault()
connection.send("Hello world!") connection.send("Hello world!")
@ -27,20 +46,27 @@ const Chat = (): React.ReactElement => {
connection.addEventListener("message", (ev: MessageEvent) => { connection.addEventListener("message", (ev: MessageEvent) => {
ev.preventDefault() ev.preventDefault()
const wrappers = document.getElementsByClassName(msgWrapperClassName) 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++) { for (let index = 0; index < wrappers.length; index++) {
const element: Element | null = wrappers.item(index); const element: Element | null = wrappers.item(index);
if (!element) { if (!element) {
console.error("msgWrapper class cannot be found! Message not delivered.") console.error("msgWrapper class cannot be found! Message not delivered.")
return return
} }
messageElementsArray.push(<Message sender={data.from} text={data.message} />)
// TODO Create new message
// DDL 20 DEC
} }
}) })
return ( return (
<div className="chat"> <div className="chat">
<div className={msgWrapperClassName}> <div className={msgWrapperClassName}>
{messageElementsArray}
</div> </div>
<input></input> <span><input id="data-entry"></input><button onClick={ev => sendDataButtonHandler}></button></span>
</div> </div>
) )
} }
export default Chat;

View file

@ -6,6 +6,14 @@ enum FileType {
EXEC_BINARY, EXEC_BINARY,
UNKNOWN, UNKNOWN,
} }
export type User = {
name: string,
userId: string,
avatar: Avatar
}
export type Avatar = {
pictureUri: string,
}
export type Attachment = { export type Attachment = {
name: string, name: string,
uri: string, uri: string,
@ -21,3 +29,6 @@ export type ChatMessage = {
message: string, message: string,
attachments: Attachment attachments: Attachment
} }
export type ServerMsgType = {
}