From d46adc06c5e368c0ecc614569da52c082fb4a3be Mon Sep 17 00:00:00 2001 From: Ayane Satomi Date: Mon, 23 Oct 2023 10:16:08 +0000 Subject: [PATCH] re-set connection handlers on reconnection --- islands/CounterCard.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/islands/CounterCard.tsx b/islands/CounterCard.tsx index c8e71cc..d8828e1 100644 --- a/islands/CounterCard.tsx +++ b/islands/CounterCard.tsx @@ -98,9 +98,7 @@ export default function Counter(props: SharedProps) { }, 5000)); }; - useEffect(() => { - let ws = new WebSocket(window.location.href.replace("http", "ws")); - + const handleWSEvents = (ws: WebSocket) => { ws.onopen = () => { console.log( `[${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...`,); ws = new WebSocket(window.location.href.replace("http", "ws")); }; + } + + useEffect(() => { + let ws = new WebSocket(window.location.href.replace("http", "ws")); + handleWSEvents(ws); const onlineHandler = () => { console.log("Client detected online, resuming connection."); ws = new WebSocket(window.location.href.replace("http", "ws")); + handleWSEvents(ws); }; const offlineHandler = () => {