Skip to content

Commit 174b261

Browse files
author
Dylan Huang
committed
Improve WebSocket connection handling
- Updated the WebSocket connection logic to prevent multiple connections by checking for both OPEN and CONNECTING states. - This change addresses potential issues in React strict mode where multiple connection attempts could occur.
1 parent fd70d4f commit 174b261

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

vite-app/src/App.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ const App = observer(() => {
2323
const reconnectAttemptsRef = useRef(0);
2424

2525
const connectWebSocket = () => {
26-
if (wsRef.current?.readyState === WebSocket.OPEN) {
27-
return; // Already connected
26+
if (
27+
wsRef.current?.readyState === WebSocket.OPEN ||
28+
wsRef.current?.readyState === WebSocket.CONNECTING
29+
) {
30+
return; // Already connected or connecting. This will happen in React strict mode.
2831
}
2932

3033
const ws = new WebSocket(getWebSocketUrl());

0 commit comments

Comments
 (0)