Skip to content

Commit

Permalink
Add reconnect reason and signal rtt calculation (#573)
Browse files Browse the repository at this point in the history
* Add reconnect reason and signal rtt caculation

* changeset
  • Loading branch information
cnderrauber authored Feb 7, 2023
1 parent c140888 commit b71ec61
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-moles-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Add reconnect reason and signal rtt calculation
36 changes: 34 additions & 2 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ClientInfo,
DisconnectReason,
ParticipantInfo,
ReconnectReason,
Room,
SpeakerInfo,
VideoLayer,
Expand Down Expand Up @@ -40,6 +41,9 @@ interface ConnectOpts {
/** internal */
reconnect?: boolean;

/** internal */
reconnectReason?: number;

/** internal */
sid?: string;

Expand Down Expand Up @@ -89,6 +93,9 @@ export class SignalClient {

useJSON: boolean;

/** signal rtt in milliseconds */
rtt: number = 0;

/** simulate signaling latency by delaying messages */
signalLatency?: number;

Expand Down Expand Up @@ -166,7 +173,12 @@ export class SignalClient {
return res as JoinResponse;
}

async reconnect(url: string, token: string, sid?: string): Promise<ReconnectResponse | void> {
async reconnect(
url: string,
token: string,
sid?: string,
reason?: ReconnectReason,
): Promise<ReconnectResponse | void> {
if (!this.options) {
log.warn('attempted to reconnect without signal options being set, ignoring');
return;
Expand All @@ -175,7 +187,12 @@ export class SignalClient {
// clear ping interval and restart it once reconnected
this.clearPingInterval();

const res = await this.connect(url, token, { ...this.options, reconnect: true, sid });
const res = await this.connect(url, token, {
...this.options,
reconnect: true,
sid,
reconnectReason: reason,
});
return res;
}

Expand Down Expand Up @@ -440,10 +457,18 @@ export class SignalClient {
}

sendPing() {
/** send both of ping and pingReq for compatibility to old and new server */
this.sendRequest({
$case: 'ping',
ping: Date.now(),
});
this.sendRequest({
$case: 'pingReq',
pingReq: {
timestamp: Date.now(),
rtt: this.rtt,
},
});
}

async sendLeave() {
Expand Down Expand Up @@ -563,6 +588,9 @@ export class SignalClient {
}
} else if (msg.$case === 'pong') {
this.resetPingTimeout();
} else if (msg.$case === 'pongResp') {
this.rtt = Date.now() - msg.pongResp.lastPingTimestamp;
this.clearPingTimeout();
} else {
log.debug('unsupported message', msg);
}
Expand Down Expand Up @@ -698,6 +726,10 @@ function createConnectionParams(token: string, info: ClientInfo, opts: ConnectOp
params.set('adaptive_stream', '1');
}

if (opts.reconnectReason) {
params.set('reconnect_reason', opts.reconnectReason.toString());
}

// @ts-ignore
if (navigator.connection?.type) {
// @ts-ignore
Expand Down
51 changes: 51 additions & 0 deletions src/proto/livekit_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,57 @@ export function disconnectReasonToJSON(object: DisconnectReason): string {
}
}

export enum ReconnectReason {
REASON_UNKOWN = 0,
REASON_SIGNAL_DISCONNECTED = 1,
REASON_PUBLISHER_FAILED = 2,
REASON_SUBSCRIBER_FAILED = 3,
REASON_SWITCH_CANDIDATE = 4,
UNRECOGNIZED = -1,
}

export function reconnectReasonFromJSON(object: any): ReconnectReason {
switch (object) {
case 0:
case "REASON_UNKOWN":
return ReconnectReason.REASON_UNKOWN;
case 1:
case "REASON_SIGNAL_DISCONNECTED":
return ReconnectReason.REASON_SIGNAL_DISCONNECTED;
case 2:
case "REASON_PUBLISHER_FAILED":
return ReconnectReason.REASON_PUBLISHER_FAILED;
case 3:
case "REASON_SUBSCRIBER_FAILED":
return ReconnectReason.REASON_SUBSCRIBER_FAILED;
case 4:
case "REASON_SWITCH_CANDIDATE":
return ReconnectReason.REASON_SWITCH_CANDIDATE;
case -1:
case "UNRECOGNIZED":
default:
return ReconnectReason.UNRECOGNIZED;
}
}

export function reconnectReasonToJSON(object: ReconnectReason): string {
switch (object) {
case ReconnectReason.REASON_UNKOWN:
return "REASON_UNKOWN";
case ReconnectReason.REASON_SIGNAL_DISCONNECTED:
return "REASON_SIGNAL_DISCONNECTED";
case ReconnectReason.REASON_PUBLISHER_FAILED:
return "REASON_PUBLISHER_FAILED";
case ReconnectReason.REASON_SUBSCRIBER_FAILED:
return "REASON_SUBSCRIBER_FAILED";
case ReconnectReason.REASON_SWITCH_CANDIDATE:
return "REASON_SWITCH_CANDIDATE";
case ReconnectReason.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}

export interface Room {
sid: string;
name: string;
Expand Down
Loading

0 comments on commit b71ec61

Please sign in to comment.