Fix error connection
This commit is contained in:
parent
22913be04e
commit
a4bf5b4579
1 changed files with 14 additions and 7 deletions
|
@ -160,7 +160,11 @@ const useSocket = (
|
||||||
wsURL.search = searchParams.toString();
|
wsURL.search = searchParams.toString();
|
||||||
|
|
||||||
const ws = new WebSocket(wsURL.toString());
|
const ws = new WebSocket(wsURL.toString());
|
||||||
|
const keepAliveInterval = setInterval(() => {
|
||||||
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
||||||
|
ws.send(JSON.stringify({ type: 'ping' }));
|
||||||
|
}
|
||||||
|
}, 50000);
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
if (ws.readyState !== 1) {
|
if (ws.readyState !== 1) {
|
||||||
toast.error(
|
toast.error(
|
||||||
|
@ -176,16 +180,20 @@ const useSocket = (
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onerror = () => {
|
ws.onerror = () => {
|
||||||
|
clearInterval(keepAliveInterval);
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
setError(true);
|
setError(true);
|
||||||
toast.error('WebSocket connection error.');
|
toast.error('WebSocket connection error.');
|
||||||
};
|
};
|
||||||
|
|
||||||
// ws.onclose = () => {
|
ws.onclose = () => {
|
||||||
// clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
// setError(true);
|
setError(true);
|
||||||
// console.log('[DEBUG] closed');
|
console.log('[DEBUG] closed');
|
||||||
// };
|
setTimeout(() => {
|
||||||
|
connectWs();
|
||||||
|
}, 2000);
|
||||||
|
};
|
||||||
|
|
||||||
ws.addEventListener('message', (e) => {
|
ws.addEventListener('message', (e) => {
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
|
@ -529,7 +537,6 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
console.log(hasError);
|
|
||||||
|
|
||||||
// if (hasError) {
|
// if (hasError) {
|
||||||
// return (
|
// return (
|
||||||
|
|
Loading…
Add table
Reference in a new issue