diff --git a/islands/IsOnline.tsx b/islands/IsOnline.tsx index 8007f3f..e5dbfc2 100644 --- a/islands/IsOnline.tsx +++ b/islands/IsOnline.tsx @@ -32,7 +32,7 @@ export default function IsOnline( ? navigator.connection as NetworkConnection : null useEffect(() => { - openSocket(status, { id: undefined }) + openSocket(status) connection?.addEventListener('change', () => { fetch('/api/serviceworker/is-online') .then(() => status.value = true) @@ -43,18 +43,19 @@ export default function IsOnline( return <>{displayed} } -function openSocket(status: Signal, ref: { id: number | undefined }) { +function openSocket(status: Signal) { const socket = new WebSocket( `wss://${location.host}/api/serviceworker/is-online`, ) socket.addEventListener('open', () => { status.value = true - clearInterval(ref.id) + const id = Number(sessionStorage.getItem('$cohabit.is-online.id') ?? -1) + clearInterval(id) }) socket.addEventListener('close', () => { status.value = false // Try reconnect every 5s - const id = setInterval(() => openSocket(status, ref), 5_000) - ref.id = id + const id = setInterval(() => openSocket(status), 5_000) + sessionStorage.setItem('$cohabit.is-online.id', id.toString()) }) }