From 045249a5a7d61c19e82c490632498ebc00ea62c0 Mon Sep 17 00:00:00 2001 From: ynwd <10122431+ynwd@users.noreply.github.com> Date: Sat, 16 Nov 2024 21:38:46 +0700 Subject: [PATCH] chore: update socket --- modules/hook/socket.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/hook/socket.ts b/modules/hook/socket.ts index 3e861729..a137952d 100644 --- a/modules/hook/socket.ts +++ b/modules/hook/socket.ts @@ -15,10 +15,16 @@ const useWebSocket = (url: string, room: string, user: string) => { setIsConnected(true); return clearInterval(i); } - socketRef.current?.send( - JSON.stringify({ ...{ type: "ping", room, user }, ...data }), - ); - countRef.current++; + + if (socketRef.current?.readyState === WebSocket.OPEN) { + socketRef.current?.send( + JSON.stringify({ + ...{ type: "ping", room, user }, + ...data, + }), + ); + countRef.current++; + } }, 1000); console.log(`WebSocket connection established: ${room}`);