re-set connection handlers on reconnection

This commit is contained in:
Ayane Satomi 2023-10-23 10:16:08 +00:00
parent 2ab8369d4e
commit d46adc06c5

View file

@ -98,9 +98,7 @@ export default function Counter(props: SharedProps) {
}, 5000)); }, 5000));
}; };
useEffect(() => { const handleWSEvents = (ws: WebSocket) => {
let ws = new WebSocket(window.location.href.replace("http", "ws"));
ws.onopen = () => { ws.onopen = () => {
console.log( console.log(
`[${new Date().toISOString()}] Connected to statistics socket`, `[${new Date().toISOString()}] Connected to statistics socket`,
@ -123,10 +121,16 @@ export default function Counter(props: SharedProps) {
console.error(`[${new Date().toISOString()}] Socket Errored. Aggressively reconnecting...`,); console.error(`[${new Date().toISOString()}] Socket Errored. Aggressively reconnecting...`,);
ws = new WebSocket(window.location.href.replace("http", "ws")); ws = new WebSocket(window.location.href.replace("http", "ws"));
}; };
}
useEffect(() => {
let ws = new WebSocket(window.location.href.replace("http", "ws"));
handleWSEvents(ws);
const onlineHandler = () => { const onlineHandler = () => {
console.log("Client detected online, resuming connection."); console.log("Client detected online, resuming connection.");
ws = new WebSocket(window.location.href.replace("http", "ws")); ws = new WebSocket(window.location.href.replace("http", "ws"));
handleWSEvents(ws);
}; };
const offlineHandler = () => { const offlineHandler = () => {