1
Fork 0

Handle ID_RESPONSE

This commit is contained in:
Tobias Berger 2022-01-08 17:36:37 +01:00
parent 0cf665997a
commit 651bee32ca
2 changed files with 8 additions and 4 deletions

@ -1 +1 @@
Subproject commit f7cfeb92bd3b6f7387e712595ec16ed02776e0cf
Subproject commit 4c698768def503209283af5d7a04c0d21c49092f

View file

@ -6,6 +6,7 @@ import useWebSocket, {
import MessageComponent from "src/components/Message";
import type { TextMessage } from "src/lib/ServerMessage";
import {
isIdResponseMessage,
isServerMessage,
isTextMessage,
MessageType,
@ -15,6 +16,7 @@ export default function Index(): JSX.Element {
const [count, setCount] = useState(0);
const [messageHistory, setMessageHistory] = useState<TextMessage[]>([]);
const [socketUrl, setSocketUrl] = useState("wss.tobot.tk:8085/");
const [authorId, setAuthorId] = useState("<???>");
function onMessage(event: MessageEvent<string>): void {
const message = JSON.parse(event.data);
@ -24,7 +26,9 @@ export default function Index(): JSX.Element {
throw new Error(`Server sent unexpected message \`${event.data}}\``);
}
if (isTextMessage(message)) {
if (isIdResponseMessage(message)) {
setAuthorId(message.authorId);
} else if (isTextMessage(message)) {
setMessageHistory([message, ...messageHistory]);
}
}
@ -48,13 +52,13 @@ export default function Index(): JSX.Element {
const handleClickSendMessage = useCallback(() => {
setCount(count + 1);
const message: TextMessage = {
author: -1,
author: authorId,
type: MessageType.TEXT,
date: Date.now(),
content: `Hello, world! ${count}`,
};
websocket.sendJsonMessage(message);
}, [count, websocket]);
}, [count, websocket, authorId]);
const trySetSocketUrl = useCallback(
(url: string) => {