From bec386b9f5b0f7dc4895bc2c11135039760896b4 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Wed, 28 Feb 2024 09:01:15 +0100 Subject: [PATCH] Emit Restarting as soon as both signal and pc connection are severed (#1047) * Emit Restarting as soon as both signal and pc connection are severed * Create three-elephants-heal.md --- .changeset/three-elephants-heal.md | 5 +++++ src/room/RTCEngine.ts | 14 ++++++++++++++ src/room/Room.ts | 5 +++++ src/room/events.ts | 1 + 4 files changed, 25 insertions(+) create mode 100644 .changeset/three-elephants-heal.md diff --git a/.changeset/three-elephants-heal.md b/.changeset/three-elephants-heal.md new file mode 100644 index 0000000000..b7af4146e4 --- /dev/null +++ b/.changeset/three-elephants-heal.md @@ -0,0 +1,5 @@ +--- +"livekit-client": patch +--- + +Emit Restarting as soon as both signal and pc connection are severed diff --git a/src/room/RTCEngine.ts b/src/room/RTCEngine.ts index e4ef50d209..fa71915314 100644 --- a/src/room/RTCEngine.ts +++ b/src/room/RTCEngine.ts @@ -418,6 +418,19 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit ); } } + + // detect cases where both signal client and peer connection are severed and assume that user has lost network connection + const isSignalSevered = + this.client.isDisconnected || + this.client.currentState === SignalConnectionState.RECONNECTING; + const isPCSevered = [ + PCTransportState.FAILED, + PCTransportState.CLOSING, + PCTransportState.CLOSED, + ].includes(connectionState); + if (isSignalSevered && isPCSevered && !this._isClosed) { + this.emit(EngineEvent.Offline); + } }; this.pcManager.onTrack = (ev: RTCTrackEvent) => { this.emit(EngineEvent.MediaTrackAdded, ev.track, ev.streams[0], ev.receiver); @@ -1400,4 +1413,5 @@ export type EngineEventCallbacks = { subscribedQualityUpdate: (update: SubscribedQualityUpdate) => void; localTrackUnpublished: (unpublishedResponse: TrackUnpublishedResponse) => void; remoteMute: (trackSid: string, muted: boolean) => void; + offline: () => void; }; diff --git a/src/room/Room.ts b/src/room/Room.ts index 1760a04802..6c006309eb 100644 --- a/src/room/Room.ts +++ b/src/room/Room.ts @@ -349,6 +349,11 @@ class Room extends (EventEmitter as new () => TypedEmitter) }) .on(EngineEvent.Restarting, this.handleRestarting) .on(EngineEvent.SignalRestarted, this.handleSignalRestarted) + .on(EngineEvent.Offline, () => { + if (this.setAndEmitConnectionState(ConnectionState.Reconnecting)) { + this.emit(RoomEvent.Reconnecting); + } + }) .on(EngineEvent.DCBufferStatusChanged, (status, kind) => { this.emit(RoomEvent.DCBufferStatusChanged, status, kind); }); diff --git a/src/room/events.ts b/src/room/events.ts index c50e4cbcb9..cc8cc614b7 100644 --- a/src/room/events.ts +++ b/src/room/events.ts @@ -491,6 +491,7 @@ export enum EngineEvent { RemoteMute = 'remoteMute', SubscribedQualityUpdate = 'subscribedQualityUpdate', LocalTrackUnpublished = 'localTrackUnpublished', + Offline = 'offline', } export enum TrackEvent {