diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index ea9a93d3..ed41711d 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -25,8 +25,11 @@ const useSocket = ( url: string, setIsWSReady: (ready: boolean) => void, setError: (error: boolean) => void, + hasError: boolean ) => { const [ws, setWs] = useState(null); + const reconnectTimeout = useRef(0); + const reconnectAttempts = useRef(0); useEffect(() => { if (!ws) { @@ -181,6 +184,8 @@ const useSocket = ( ws.onopen = () => { console.log('[DEBUG] open'); + reconnectTimeout.current = 0; + reconnectAttempts.current = 0; clearTimeout(timeoutId); setError(false); setIsWSReady(true); @@ -194,7 +199,9 @@ const useSocket = ( ws.onclose = () => { clearTimeout(timeoutId); - setError(true); + if (!hasError && reconnectAttempts.current < 3) { + setWs(null); // forces websocket to reopen when needed. + } console.log('[DEBUG] closed'); }; @@ -208,7 +215,15 @@ const useSocket = ( setWs(ws); }; - connectWs(); + if (reconnectAttempts.current < 3) { + console.log(`[DEBUG] Attempting to reconnect (${reconnectAttempts.current + 1}/3)`); + setTimeout(connectWs, reconnectTimeout.current); + reconnectTimeout.current = reconnectTimeout.current > 0 ? reconnectTimeout.current * 2 : 1000; + reconnectAttempts.current += 1; + } else { + console.log('[DEBUG] WebSocket reconnect failure after 3 retries'); + setError(true); + } } return () => { @@ -285,6 +300,7 @@ const ChatWindow = ({ id }: { id?: string }) => { process.env.NEXT_PUBLIC_WS_URL!, setIsWSReady, setHasError, + hasError ); const [loading, setLoading] = useState(false);