diff --git a/.changeset/silent-lamps-grin.md b/.changeset/silent-lamps-grin.md new file mode 100644 index 0000000000..450d91a447 --- /dev/null +++ b/.changeset/silent-lamps-grin.md @@ -0,0 +1,5 @@ +--- +"livekit-client": patch +--- + +Signal leave on failed connection attempts if signalling is connected diff --git a/src/room/Room.ts b/src/room/Room.ts index 7de0c84fad..f7a6fc85bc 100644 --- a/src/room/Room.ts +++ b/src/room/Room.ts @@ -740,7 +740,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) `Initial connection failed with ConnectionError: ${error.message}. Retrying with another region: ${nextUrl}`, this.logContext, ); - this.recreateEngine(); + this.recreateEngine(true); await connectFn(resolve, reject, nextUrl); } else { this.handleDisconnect( @@ -866,7 +866,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) ) { this.log.info('Reconnection attempt replaced by new connection attempt', this.logContext); // make sure we close and recreate the existing engine in order to get rid of any potentially ongoing reconnection attempts - this.recreateEngine(); + this.recreateEngine(true); } else { // create engine if previously disconnected this.maybeCreateEngine(); @@ -1380,8 +1380,14 @@ class Room extends (EventEmitter as new () => TypedEmitter) ); } - private recreateEngine() { - this.engine?.close(); + private recreateEngine(sendLeave?: boolean) { + const oldEngine = this.engine; + + if (sendLeave && oldEngine && !oldEngine.client.isDisconnected) { + oldEngine.client.sendLeave().finally(() => oldEngine.close()); + } else { + oldEngine?.close(); + } /* @ts-ignore */ this.engine = undefined; this.isResuming = false;