From 7b796aee23c398574c2274ddf958c9b40ffa8581 Mon Sep 17 00:00:00 2001 From: Aurastic <33085835+ISenseAura@users.noreply.github.com> Date: Wed, 28 May 2025 05:02:51 +0530 Subject: [PATCH] Preact: Fix autoreconnect (#2445) --------- Co-authored-by: Guangcong Luo --- .../src/client-connection-worker.ts | 2 +- play.pokemonshowdown.com/src/client-connection.ts | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/play.pokemonshowdown.com/src/client-connection-worker.ts b/play.pokemonshowdown.com/src/client-connection-worker.ts index 1fce66f7c..8cbdfe007 100644 --- a/play.pokemonshowdown.com/src/client-connection-worker.ts +++ b/play.pokemonshowdown.com/src/client-connection-worker.ts @@ -52,7 +52,7 @@ function connectToServer() { }; socket.onerror = (err: Event) => { - postMessage({ type: 'error', err }); + postMessage({ type: 'error', data: (err as any).message || '' }); socket?.close(); }; return; diff --git a/play.pokemonshowdown.com/src/client-connection.ts b/play.pokemonshowdown.com/src/client-connection.ts index c59da1eac..488276487 100644 --- a/play.pokemonshowdown.com/src/client-connection.ts +++ b/play.pokemonshowdown.com/src/client-connection.ts @@ -52,7 +52,7 @@ export class PSConnection { if (this.socket) return false; // must be one or the other try { - const worker = new Worker('/js/reconnect-worker.js'); + const worker = new Worker('/js/client-connection-worker.js'); this.worker = worker; worker.postMessage({ type: 'connect', server: PS.server }); @@ -75,9 +75,11 @@ export class PSConnection { this.handleDisconnect(); break; case 'error': - console.warn('Worker connection error'); + console.warn(`Worker connection error: ${data}`); this.worker = null; - this.directConnect(); // fallback + // onerror can occur on abrupt disconnects or fatal errors. + // handleDisconnect ensures proper cleanup and also attemps to reconnect. + this.handleDisconnect(); // fallback break; } }; @@ -165,10 +167,11 @@ export class PSConnection { private retryConnection() { if (!this.canReconnect()) return; + if (this.reconnectTimer) return; this.reconnectTimer = setTimeout(() => { this.reconnectTimer = null; if (!this.connected && this.canReconnect()) { - PS.send('/reconnect'); + PS.mainmenu.send('/reconnect'); this.reconnectDelay = Math.min(this.reconnectDelay * 2, this.reconnectCap); } }, this.reconnectDelay);