diff --git a/src/components/ChatWindow/ChatWindow.ts b/src/components/ChatWindow/ChatWindow.ts index 5bc60e0c..c7e81fec 100644 --- a/src/components/ChatWindow/ChatWindow.ts +++ b/src/components/ChatWindow/ChatWindow.ts @@ -3,6 +3,11 @@ import globalStore from '../../modules/GlobalStore'; import { convertTimeToMinutesAndSeconds } from '../../modules/Utils'; import { Message } from '../../repositories/ChatRepository'; +interface BackendReply { + response: string; + sent: boolean; +} + export default class ChatWindow extends BaseComponent { private messages: Message[]; private messagesContainer: HTMLDivElement; @@ -54,10 +59,7 @@ export default class ChatWindow extends BaseComponent { console.log('Отправляем данные на сервер'); }; - socket.onmessage = (event) => { - console.log(`[message] Данные получены с сервера: ${event.data}`); - this.addNewMessageElement(JSON.parse(event.data) as Message); - }; + socket.onmessage = (e) => this.handleMessageReceive(e); socket.onclose = function (event) { if (event.wasClean) { @@ -121,6 +123,7 @@ export default class ChatWindow extends BaseComponent { content: text, }) ); + this.addNewMessageElement({ content: text, receiverId: '', @@ -130,6 +133,24 @@ export default class ChatWindow extends BaseComponent { }); } + private handleMessageReceive(event: MessageEvent) { + let message; + + try { + message = JSON.parse(event.data) as Message | BackendReply; + } catch { + console.warn('Received non-JSON message! Ignoring it'); + return; + } + + console.log(`[message] Данные получены с сервера: ${message}`); + if ('content' in message) { + this.addNewMessageElement(message); + } else { + console.log('other type of message'); + } + } + private addNewMessageElement(message: Message) { const template = document.getElementById( 'js-chat-message-template'