5
5
ClientInfo ,
6
6
DisconnectReason ,
7
7
ParticipantInfo ,
8
+ ReconnectReason ,
8
9
Room ,
9
10
SpeakerInfo ,
10
11
VideoLayer ,
@@ -40,6 +41,9 @@ interface ConnectOpts {
40
41
/** internal */
41
42
reconnect ?: boolean ;
42
43
44
+ /** internal */
45
+ reconnectReason ?: number ;
46
+
43
47
/** internal */
44
48
sid ?: string ;
45
49
@@ -89,6 +93,9 @@ export class SignalClient {
89
93
90
94
useJSON : boolean ;
91
95
96
+ /** signal rtt in milliseconds */
97
+ rtt : number = 0 ;
98
+
92
99
/** simulate signaling latency by delaying messages */
93
100
signalLatency ?: number ;
94
101
@@ -166,7 +173,12 @@ export class SignalClient {
166
173
return res as JoinResponse ;
167
174
}
168
175
169
- async reconnect ( url : string , token : string , sid ?: string ) : Promise < ReconnectResponse | void > {
176
+ async reconnect (
177
+ url : string ,
178
+ token : string ,
179
+ sid ?: string ,
180
+ reason ?: ReconnectReason ,
181
+ ) : Promise < ReconnectResponse | void > {
170
182
if ( ! this . options ) {
171
183
log . warn ( 'attempted to reconnect without signal options being set, ignoring' ) ;
172
184
return ;
@@ -175,7 +187,12 @@ export class SignalClient {
175
187
// clear ping interval and restart it once reconnected
176
188
this . clearPingInterval ( ) ;
177
189
178
- const res = await this . connect ( url , token , { ...this . options , reconnect : true , sid } ) ;
190
+ const res = await this . connect ( url , token , {
191
+ ...this . options ,
192
+ reconnect : true ,
193
+ sid,
194
+ reconnectReason : reason ,
195
+ } ) ;
179
196
return res ;
180
197
}
181
198
@@ -440,10 +457,18 @@ export class SignalClient {
440
457
}
441
458
442
459
sendPing ( ) {
460
+ /** send both of ping and pingReq for compatibility to old and new server */
443
461
this . sendRequest ( {
444
462
$case : 'ping' ,
445
463
ping : Date . now ( ) ,
446
464
} ) ;
465
+ this . sendRequest ( {
466
+ $case : 'pingReq' ,
467
+ pingReq : {
468
+ timestamp : Date . now ( ) ,
469
+ rtt : this . rtt ,
470
+ } ,
471
+ } ) ;
447
472
}
448
473
449
474
async sendLeave ( ) {
@@ -563,6 +588,9 @@ export class SignalClient {
563
588
}
564
589
} else if ( msg . $case === 'pong' ) {
565
590
this . resetPingTimeout ( ) ;
591
+ } else if ( msg . $case === 'pongResp' ) {
592
+ this . rtt = Date . now ( ) - msg . pongResp . lastPingTimestamp ;
593
+ this . clearPingTimeout ( ) ;
566
594
} else {
567
595
log . debug ( 'unsupported message' , msg ) ;
568
596
}
@@ -698,6 +726,10 @@ function createConnectionParams(token: string, info: ClientInfo, opts: ConnectOp
698
726
params . set ( 'adaptive_stream' , '1' ) ;
699
727
}
700
728
729
+ if ( opts . reconnectReason ) {
730
+ params . set ( 'reconnect_reason' , opts . reconnectReason . toString ( ) ) ;
731
+ }
732
+
701
733
// @ts -ignore
702
734
if ( navigator . connection ?. type ) {
703
735
// @ts -ignore
0 commit comments