From 01ed3c68422f8f3c05f2401f5459930e79034aac Mon Sep 17 00:00:00 2001 From: Alec Date: Tue, 26 Mar 2024 22:01:39 -0400 Subject: [PATCH] Remove unload event (#192) * use page hide event, manually reconnect * comment * lint --- client/src/modules/page_handlers/gameHandler.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/src/modules/page_handlers/gameHandler.js b/client/src/modules/page_handlers/gameHandler.js index 499112b..b4db01b 100644 --- a/client/src/modules/page_handlers/gameHandler.js +++ b/client/src/modules/page_handlers/gameHandler.js @@ -18,7 +18,7 @@ import { InProgress } from '../game_state/states/InProgress.js'; import { Ended } from '../game_state/states/Ended.js'; export const gameHandler = (socket, window, gameDOM) => { - window.onunload = () => { + window.onpagehide = () => { socket.close(); }; document.body.innerHTML = gameDOM + document.body.innerHTML; @@ -60,8 +60,11 @@ export const gameHandler = (socket, window, gameDOM) => { toast(err, 'error', true, false); }); - socket.on('disconnect', () => { + socket.on('disconnect', (reason) => { toast('Disconnected. Attempting reconnect...', 'error', true, false); + if (reason === 'io client disconnect') { // see https://socket.io/docs/v4/client-api/#event-disconnect + socket.connect(); + } }); setClientSocketHandlers(stateBucket, socket); resolve();