diff --git a/packages/server-sdk/src/client.ts b/packages/server-sdk/src/client.ts index 068b348b2..6ef0a49c6 100644 --- a/packages/server-sdk/src/client.ts +++ b/packages/server-sdk/src/client.ts @@ -52,6 +52,12 @@ export class Client { } }) + 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 { 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() + } }