Skip to content

Commit 73932e6

Browse files
authored
Signal leave on failed connection attempts if signalling is connected (livekit#1817)
1 parent 91824ab commit 73932e6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

.changeset/silent-lamps-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-client": patch
3+
---
4+
5+
Signal leave on failed connection attempts if signalling is connected

src/room/Room.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
740740
`Initial connection failed with ConnectionError: ${error.message}. Retrying with another region: ${nextUrl}`,
741741
this.logContext,
742742
);
743-
this.recreateEngine();
743+
this.recreateEngine(true);
744744
await connectFn(resolve, reject, nextUrl);
745745
} else {
746746
this.handleDisconnect(
@@ -866,7 +866,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
866866
) {
867867
this.log.info('Reconnection attempt replaced by new connection attempt', this.logContext);
868868
// make sure we close and recreate the existing engine in order to get rid of any potentially ongoing reconnection attempts
869-
this.recreateEngine();
869+
this.recreateEngine(true);
870870
} else {
871871
// create engine if previously disconnected
872872
this.maybeCreateEngine();
@@ -1380,8 +1380,14 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
13801380
);
13811381
}
13821382

1383-
private recreateEngine() {
1384-
this.engine?.close();
1383+
private recreateEngine(sendLeave?: boolean) {
1384+
const oldEngine = this.engine;
1385+
1386+
if (sendLeave && oldEngine && !oldEngine.client.isDisconnected) {
1387+
oldEngine.client.sendLeave().finally(() => oldEngine.close());
1388+
} else {
1389+
oldEngine?.close();
1390+
}
13851391
/* @ts-ignore */
13861392
this.engine = undefined;
13871393
this.isResuming = false;

0 commit comments

Comments
 (0)