Skip to content

Commit

Permalink
Emit Restarting as soon as both signal and pc connection are severed (#…
Browse files Browse the repository at this point in the history
…1047)

* Emit Restarting as soon as both signal and pc connection are severed

* Create three-elephants-heal.md
  • Loading branch information
lukasIO authored Feb 28, 2024
1 parent 81bde6d commit bec386b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-elephants-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Emit Restarting as soon as both signal and pc connection are severed
14 changes: 14 additions & 0 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1400,4 +1413,5 @@ export type EngineEventCallbacks = {
subscribedQualityUpdate: (update: SubscribedQualityUpdate) => void;
localTrackUnpublished: (unpublishedResponse: TrackUnpublishedResponse) => void;
remoteMute: (trackSid: string, muted: boolean) => void;
offline: () => void;
};
5 changes: 5 additions & 0 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
})
.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);
});
Expand Down
1 change: 1 addition & 0 deletions src/room/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ export enum EngineEvent {
RemoteMute = 'remoteMute',
SubscribedQualityUpdate = 'subscribedQualityUpdate',
LocalTrackUnpublished = 'localTrackUnpublished',
Offline = 'offline',
}

export enum TrackEvent {
Expand Down

0 comments on commit bec386b

Please sign in to comment.