Handle ID_RESPONSE
This commit is contained in:
parent
0cf665997a
commit
651bee32ca
2 changed files with 8 additions and 4 deletions
2
src/lib
2
src/lib
|
@ -1 +1 @@
|
||||||
Subproject commit f7cfeb92bd3b6f7387e712595ec16ed02776e0cf
|
Subproject commit 4c698768def503209283af5d7a04c0d21c49092f
|
|
@ -6,6 +6,7 @@ import useWebSocket, {
|
||||||
import MessageComponent from "src/components/Message";
|
import MessageComponent from "src/components/Message";
|
||||||
import type { TextMessage } from "src/lib/ServerMessage";
|
import type { TextMessage } from "src/lib/ServerMessage";
|
||||||
import {
|
import {
|
||||||
|
isIdResponseMessage,
|
||||||
isServerMessage,
|
isServerMessage,
|
||||||
isTextMessage,
|
isTextMessage,
|
||||||
MessageType,
|
MessageType,
|
||||||
|
@ -15,6 +16,7 @@ export default function Index(): JSX.Element {
|
||||||
const [count, setCount] = useState(0);
|
const [count, setCount] = useState(0);
|
||||||
const [messageHistory, setMessageHistory] = useState<TextMessage[]>([]);
|
const [messageHistory, setMessageHistory] = useState<TextMessage[]>([]);
|
||||||
const [socketUrl, setSocketUrl] = useState("wss.tobot.tk:8085/");
|
const [socketUrl, setSocketUrl] = useState("wss.tobot.tk:8085/");
|
||||||
|
const [authorId, setAuthorId] = useState("<???>");
|
||||||
|
|
||||||
function onMessage(event: MessageEvent<string>): void {
|
function onMessage(event: MessageEvent<string>): void {
|
||||||
const message = JSON.parse(event.data);
|
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}}\``);
|
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]);
|
setMessageHistory([message, ...messageHistory]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,13 +52,13 @@ export default function Index(): JSX.Element {
|
||||||
const handleClickSendMessage = useCallback(() => {
|
const handleClickSendMessage = useCallback(() => {
|
||||||
setCount(count + 1);
|
setCount(count + 1);
|
||||||
const message: TextMessage = {
|
const message: TextMessage = {
|
||||||
author: -1,
|
author: authorId,
|
||||||
type: MessageType.TEXT,
|
type: MessageType.TEXT,
|
||||||
date: Date.now(),
|
date: Date.now(),
|
||||||
content: `Hello, world! ${count}`,
|
content: `Hello, world! ${count}`,
|
||||||
};
|
};
|
||||||
websocket.sendJsonMessage(message);
|
websocket.sendJsonMessage(message);
|
||||||
}, [count, websocket]);
|
}, [count, websocket, authorId]);
|
||||||
|
|
||||||
const trySetSocketUrl = useCallback(
|
const trySetSocketUrl = useCallback(
|
||||||
(url: string) => {
|
(url: string) => {
|
||||||
|
|
Reference in a new issue