Skip to content

Commit

Permalink
Update to protocol 13 with LeaveRequest Action (#1127)
Browse files Browse the repository at this point in the history
* Update to protocol 13 with LeaveRequest Action

* Create selfish-moles-develop.md

* update leave requests to use action

* remove unrelated

* add comment
  • Loading branch information
lukasIO authored May 6, 2024
1 parent 85daf8d commit 3bcfee8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-moles-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Update to protocol 13 with LeaveRequest Action
4 changes: 3 additions & 1 deletion src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DisconnectReason,
JoinResponse,
LeaveRequest,
LeaveRequest_Action,
MuteTrackRequest,
ParticipantInfo,
Ping,
Expand Down Expand Up @@ -596,8 +597,9 @@ export class SignalClient {
return this.sendRequest({
case: 'leave',
value: new LeaveRequest({
canReconnect: false,
reason: DisconnectReason.CLIENT_INITIATED,
// server doesn't process this field, keeping it here to indicate the intent of a full disconnect
action: LeaveRequest_Action.DISCONNECT,
}),
});
}
Expand Down
31 changes: 22 additions & 9 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DisconnectReason,
type JoinResponse,
type LeaveRequest,
LeaveRequest_Action,
ParticipantInfo,
ReconnectReason,
type ReconnectResponse,
Expand Down Expand Up @@ -504,16 +505,28 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
this.handleDisconnect('signal', ReconnectReason.RR_SIGNAL_DISCONNECTED);
};

this.client.onLeave = (leave?: LeaveRequest) => {
if (leave?.canReconnect) {
this.fullReconnectOnNext = true;
// reconnect immediately instead of waiting for next attempt
this.handleDisconnect(leaveReconnect);
} else {
this.emit(EngineEvent.Disconnected, leave?.reason);
this.close();
}
this.client.onLeave = (leave: LeaveRequest) => {
this.log.debug('client leave request', { ...this.logContext, reason: leave?.reason });
if (leave.regions && this.regionUrlProvider) {
this.log.debug('updating regions', this.logContext);
this.regionUrlProvider.setServerReportedRegions(leave.regions);
}
switch (leave.action) {
case LeaveRequest_Action.DISCONNECT:
this.emit(EngineEvent.Disconnected, leave?.reason);
this.close();
break;
case LeaveRequest_Action.RECONNECT:
this.fullReconnectOnNext = true;
// reconnect immediately instead of waiting for next attempt
this.handleDisconnect(leaveReconnect);
break;
case LeaveRequest_Action.RESUME:
// reconnect immediately instead of waiting for next attempt
this.handleDisconnect(leaveReconnect);
default:
break;
}
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/room/RegionUrlProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export class RegionUrlProvider {
);
}
}

setServerReportedRegions(regions: RegionSettings) {
this.regionSettings = regions;
this.lastUpdateAt = Date.now();
}
}

function getCloudConfigUrl(serverUrl: URL) {
Expand Down
3 changes: 2 additions & 1 deletion src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DisconnectReason,
JoinResponse,
LeaveRequest,
LeaveRequest_Action,
ParticipantInfo,
ParticipantInfo_State,
ParticipantPermission,
Expand Down Expand Up @@ -859,7 +860,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
onLeave(
new LeaveRequest({
reason: DisconnectReason.CLIENT_INITIATED,
canReconnect: true,
action: LeaveRequest_Action.RECONNECT,
}),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { version as v } from '../package.json';

export const version = v;
export const protocolVersion = 12;
export const protocolVersion = 13;

0 comments on commit 3bcfee8

Please sign in to comment.