mirror of
https://github.com/smogon/pokemon-showdown-client.git
synced 2026-03-21 17:50:29 -05:00
Preact: Fix autoreconnect (#2445)
--------- Co-authored-by: Guangcong Luo <guangcongluo@gmail.com>
This commit is contained in:
parent
e7c36480ba
commit
7b796aee23
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user