feat(chat-window): correctly handle server side WS closure
This commit is contained in:
parent
99cae076a7
commit
6d9d712790
1 changed files with 6 additions and 2 deletions
|
@ -270,12 +270,14 @@ const useSocket = (
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.debug(new Date(), 'ws:error', error);
|
console.debug(new Date(), 'ws:error', error);
|
||||||
|
setIsWSReady(false);
|
||||||
attemptReconnect();
|
attemptReconnect();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const attemptReconnect = () => {
|
const attemptReconnect = () => {
|
||||||
retryCountRef.current += 1;
|
retryCountRef.current += 1;
|
||||||
|
|
||||||
if (retryCountRef.current > MAX_RETRIES) {
|
if (retryCountRef.current > MAX_RETRIES) {
|
||||||
console.debug(new Date(), 'ws:max_retries');
|
console.debug(new Date(), 'ws:max_retries');
|
||||||
setError(true);
|
setError(true);
|
||||||
|
@ -303,14 +305,14 @@ const useSocket = (
|
||||||
connectWs();
|
connectWs();
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
isCleaningUpRef.current = true;
|
|
||||||
if (reconnectTimeoutRef.current) {
|
if (reconnectTimeoutRef.current) {
|
||||||
clearTimeout(reconnectTimeoutRef.current);
|
clearTimeout(reconnectTimeoutRef.current);
|
||||||
}
|
}
|
||||||
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
||||||
wsRef.current.close();
|
wsRef.current.close();
|
||||||
}
|
isCleaningUpRef.current = true;
|
||||||
console.debug(new Date(), 'ws:cleanup');
|
console.debug(new Date(), 'ws:cleanup');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, [url, setIsWSReady, setError]);
|
}, [url, setIsWSReady, setError]);
|
||||||
|
|
||||||
|
@ -456,6 +458,8 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
||||||
if (isMessagesLoaded && isWSReady) {
|
if (isMessagesLoaded && isWSReady) {
|
||||||
setIsReady(true);
|
setIsReady(true);
|
||||||
console.debug(new Date(), 'app:ready');
|
console.debug(new Date(), 'app:ready');
|
||||||
|
} else {
|
||||||
|
setIsReady(false);
|
||||||
}
|
}
|
||||||
}, [isMessagesLoaded, isWSReady]);
|
}, [isMessagesLoaded, isWSReady]);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue