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}`);