From 860808b392991611cefc78a9fa58fb4439a5e856 Mon Sep 17 00:00:00 2001 From: rasulov1337 <117843890+rasulov1337@users.noreply.github.com> Date: Thu, 19 Dec 2024 22:05:51 +0300 Subject: [PATCH] fix: initiate only one websocket connection instead of 1 per session --- src/components/ChatWindow/ChatWindow.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/ChatWindow/ChatWindow.ts b/src/components/ChatWindow/ChatWindow.ts index eecfdd8c..c4fc8ad7 100644 --- a/src/components/ChatWindow/ChatWindow.ts +++ b/src/components/ChatWindow/ChatWindow.ts @@ -32,13 +32,15 @@ export default class ChatWindow extends BaseComponent { this.recipientId = recipientId; this.messages = messages; - if (globalStore.chat.socket) { - globalStore.chat.socket.close(); + if ( + !globalStore.chat.socket || + globalStore.chat.socket.readyState === WebSocket.CLOSED || + globalStore.chat.socket.readyState === WebSocket.CLOSING + ) { + globalStore.chat.socket = new WebSocket( + `wss://${window.location.hostname}:${location.port}/websocket` + ); } - - globalStore.chat.socket = new WebSocket( - `wss://${window.location.hostname}:${location.port}/websocket` - ); } protected afterRender() {