mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-05-01 00:32:38 +00:00
feat(chat-window): correctly handle server side WS closure
This commit is contained in:
@ -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]);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user