Skip to content

Commit

Permalink
Always fire EngineEvent.Resuming (#600)
Browse files Browse the repository at this point in the history
* always fire EngineEvent.Resuming

* changed RTCEngine.connectedServerAddress to an async getter getConnectedServerAddress()
  • Loading branch information
davidzhao authored Mar 3, 2023
1 parent 75776b8 commit 75d7556
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-readers-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

internal getter connectedServerAddress is has been changed to an async function getConnectedServerAddress
9 changes: 6 additions & 3 deletions example/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ const appActions = {
.on(RoomEvent.DataReceived, handleData)
.on(RoomEvent.Disconnected, handleRoomDisconnect)
.on(RoomEvent.Reconnecting, () => appendLog('Reconnecting to room'))
.on(RoomEvent.Reconnected, () => {
appendLog('Successfully reconnected. server', room.engine.connectedServerAddress);
.on(RoomEvent.Reconnected, async () => {
appendLog(
'Successfully reconnected. server',
await room.engine.getConnectedServerAddress(),
);
})
.on(RoomEvent.LocalTrackPublished, (pub) => {
const track = pub.track as LocalAudioTrack;
Expand Down Expand Up @@ -182,7 +185,7 @@ const appActions = {
const elapsed = Date.now() - startTime;
appendLog(
`successfully connected to ${room.name} in ${Math.round(elapsed)}ms`,
room.engine.connectedServerAddress,
await room.engine.getConnectedServerAddress(),
);
} catch (error: any) {
let message: any = error;
Expand Down
2 changes: 2 additions & 0 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ export default class PCTransport extends EventEmitter {
}

close() {
this.pc.onconnectionstatechange = null;
this.pc.oniceconnectionstatechange = null;
this.pc.close();
}

Expand Down
24 changes: 6 additions & 18 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit

private clientConfiguration?: ClientConfiguration;

private connectedServerAddr?: string;

private attemptingReconnect: boolean = false;

private reconnectPolicy: ReconnectPolicy;
Expand Down Expand Up @@ -280,8 +278,11 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
return this.reliableDCSub?.readyState;
}

get connectedServerAddress(): string | undefined {
return this.connectedServerAddr;
async getConnectedServerAddress(): Promise<string | undefined> {
if (this.primaryPC === undefined) {
return undefined;
}
return getConnectedAddress(this.primaryPC);
}

private configure(joinResponse: JoinResponse) {
Expand Down Expand Up @@ -327,11 +328,6 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
primaryPC.onconnectionstatechange = async () => {
log.debug(`primary PC state changed ${primaryPC.connectionState}`);
if (primaryPC.connectionState === 'connected') {
try {
this.connectedServerAddr = await getConnectedAddress(primaryPC);
} catch (e) {
log.warn('could not get connected server address', { error: e });
}
const shouldEmit = this.pcState === PCState.New;
this.pcState = PCState.Connected;
if (shouldEmit) {
Expand Down Expand Up @@ -858,10 +854,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
}

log.info(`resuming signal connection, attempt ${this.reconnectAttempts}`);
// do not emit for the first attempt, since ICE restart could happen frequently
if (this.reconnectAttempts !== 0) {
this.emit(EngineEvent.Resuming);
}
this.emit(EngineEvent.Resuming);

try {
const res = await this.client.reconnect(this.url, this.token, this.participantSid, reason);
Expand Down Expand Up @@ -922,11 +915,6 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
this.primaryPC?.connectionState === 'connected'
) {
this.pcState = PCState.Connected;
try {
this.connectedServerAddr = await getConnectedAddress(this.primaryPC);
} catch (e) {
log.warn('could not get connected server address', { error: e });
}
}
if (this.pcState === PCState.Connected) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
participant.tracks.forEach((publication) => {
participant.unpublishTrack(publication.trackSid, true);
});
this.emitWhenConnected(RoomEvent.ParticipantDisconnected, participant);
this.emit(RoomEvent.ParticipantDisconnected, participant);
}

// updates are sent only when there's a change to speaker ordering
Expand Down Expand Up @@ -1124,7 +1124,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
},
)
.on(ParticipantEvent.TrackUnpublished, (publication: RemoteTrackPublication) => {
this.emitWhenConnected(RoomEvent.TrackUnpublished, publication, participant);
this.emit(RoomEvent.TrackUnpublished, publication, participant);
})
.on(
ParticipantEvent.TrackUnsubscribed,
Expand Down

0 comments on commit 75d7556

Please sign in to comment.