Preact: Fix autoreconnect (#2445)

---------

Co-authored-by: Guangcong Luo <guangcongluo@gmail.com>
This commit is contained in:
Aurastic 2025-05-28 05:02:51 +05:30 committed by GitHub
parent e7c36480ba
commit 7b796aee23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 5 deletions

View File

@ -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;

View File

@ -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);