fix(server-sdk): auto-reconnect once unauthorized

This commit is contained in:
Neko Ayaka
2025-12-20 21:40:21 +08:00
parent cc603de54d
commit 4f914d854c
+19
View File
@@ -52,6 +52,12 @@ export class Client<C = undefined> {
}
})
this.onEvent('error', async (event) => {
if (event.data.message === 'not authenticated') {
await this._reconnectDueToUnauthorized()
}
})
if (this.opts.autoConnect) {
void this.connect()
}
@@ -265,4 +271,17 @@ export class Client<C = undefined> {
this.connected = false
}
}
private async _reconnectDueToUnauthorized() {
if (this.shouldClose)
return
const ws = this.websocket
this.connected = false
if (ws && ws.readyState !== WebSocket.CLOSED && ws.readyState !== WebSocket.CLOSING) {
ws.close()
}
await this.connect()
}
}