6
6
import Logger from '../base/logger.js' ;
7
7
import * as EventModule from '../base/event.js' ;
8
8
import { ConferenceError } from './error.js' ;
9
+ import { Base64 } from '../base/base64.js' ;
9
10
10
11
'use strict' ;
11
12
13
+ const reconnectionAttempts = 10 ;
14
+
12
15
// eslint-disable-next-line require-jsdoc
13
16
function handleResponse ( status , data , resolve , reject ) {
14
17
if ( status === 'ok' || status === 'success' ) {
@@ -20,15 +23,12 @@ function handleResponse(status, data, resolve, reject) {
20
23
}
21
24
}
22
25
23
- const MAX_TRIALS = 5 ;
24
26
/**
25
27
* @class SioSignaling
26
28
* @classdesc Socket.IO signaling channel for ConferenceClient. It is not recommended to directly access this class.
27
29
* @memberof Owt.Conference
28
30
* @extends Owt.Base.EventDispatcher
29
31
* @constructor
30
- * @param {?Object } sioConfig Configuration for Socket.IO options.
31
- * @see https://socket.io/docs/client-api/#io-url-options
32
32
*/
33
33
export class SioSignaling extends EventModule . EventDispatcher {
34
34
// eslint-disable-next-line require-jsdoc
@@ -38,6 +38,7 @@ export class SioSignaling extends EventModule.EventDispatcher {
38
38
this . _loggedIn = false ;
39
39
this . _reconnectTimes = 0 ;
40
40
this . _reconnectionTicket = null ;
41
+ this . _refreshReconnectionTicket = null ;
41
42
}
42
43
43
44
/**
@@ -55,7 +56,7 @@ export class SioSignaling extends EventModule.EventDispatcher {
55
56
return new Promise ( ( resolve , reject ) => {
56
57
const opts = {
57
58
'reconnection' : true ,
58
- 'reconnectionAttempts' : MAX_TRIALS ,
59
+ 'reconnectionAttempts' : reconnectionAttempts ,
59
60
'force new connection' : true ,
60
61
} ;
61
62
this . _socket = io ( host , opts ) ;
@@ -74,30 +75,31 @@ export class SioSignaling extends EventModule.EventDispatcher {
74
75
this . _reconnectTimes ++ ;
75
76
} ) ;
76
77
this . _socket . on ( 'reconnect_failed' , ( ) => {
77
- if ( this . _reconnectTimes >= MAX_TRIALS ) {
78
+ if ( this . _reconnectTimes >= reconnectionAttempts ) {
78
79
this . dispatchEvent ( new EventModule . OwtEvent ( 'disconnect' ) ) ;
79
80
}
80
81
} ) ;
81
82
this . _socket . on ( 'drop' , ( ) => {
82
- this . _reconnectTimes = MAX_TRIALS ;
83
+ this . _reconnectTimes = reconnectionAttempts ;
83
84
} ) ;
84
85
this . _socket . on ( 'disconnect' , ( ) => {
85
- if ( this . _reconnectTimes >= MAX_TRIALS ) {
86
+ this . _clearReconnectionTask ( ) ;
87
+ if ( this . _reconnectTimes >= reconnectionAttempts ) {
86
88
this . _loggedIn = false ;
87
89
this . dispatchEvent ( new EventModule . OwtEvent ( 'disconnect' ) ) ;
88
90
}
89
91
} ) ;
90
92
this . _socket . emit ( 'login' , loginInfo , ( status , data ) => {
91
93
if ( status === 'ok' ) {
92
94
this . _loggedIn = true ;
93
- this . _reconnectionTicket = data . reconnectionTicket ;
95
+ this . _onReconnectionTicket ( data . reconnectionTicket ) ;
94
96
this . _socket . on ( 'connect' , ( ) => {
95
97
// re-login with reconnection ticket.
96
98
this . _socket . emit ( 'relogin' , this . _reconnectionTicket , ( status ,
97
99
data ) => {
98
100
if ( status === 'ok' ) {
99
101
this . _reconnectTimes = 0 ;
100
- this . _reconnectionTicket = data ;
102
+ this . _onReconnectionTicket ( data ) ;
101
103
} else {
102
104
this . dispatchEvent ( new EventModule . OwtEvent ( 'disconnect' ) ) ;
103
105
}
@@ -125,7 +127,7 @@ export class SioSignaling extends EventModule.EventDispatcher {
125
127
return new Promise ( ( resolve , reject ) => {
126
128
this . _socket . emit ( 'logout' , ( status , data ) => {
127
129
// Maximize the reconnect times to disable reconnection.
128
- this . _reconnectTimes = MAX_TRIALS ;
130
+ this . _reconnectTimes = reconnectionAttempts ;
129
131
this . _socket . disconnect ( ) ;
130
132
handleResponse ( status , data , resolve , reject ) ;
131
133
} ) ;
@@ -149,4 +151,43 @@ export class SioSignaling extends EventModule.EventDispatcher {
149
151
} ) ;
150
152
} ) ;
151
153
}
154
+
155
+ /**
156
+ * @function _onReconnectionTicket
157
+ * @instance
158
+ * @desc Parse reconnection ticket and schedule ticket refreshing.
159
+ * @memberof Owt.Conference.SioSignaling
160
+ * @private .
161
+ */
162
+ _onReconnectionTicket ( ticketString ) {
163
+ this . _reconnectionTicket = ticketString ;
164
+ const ticket = JSON . parse ( Base64 . decodeBase64 ( ticketString ) ) ;
165
+ // Refresh ticket 1 min or 10 seconds before it expires.
166
+ const now = Date . now ( ) ;
167
+ const millisecondsInOneMinute = 60 * 1000 ;
168
+ const millisecondsInTenSeconds = 10 * 1000 ;
169
+ if ( ticket . notAfter <= now - millisecondsInTenSeconds ) {
170
+ Logger . warning ( 'Reconnection ticket expires too soon.' ) ;
171
+ return ;
172
+ }
173
+ let refreshAfter = ticket . notAfter - now - millisecondsInOneMinute ;
174
+ if ( refreshAfter < 0 ) {
175
+ refreshAfter = ticket . notAfter - now - millisecondsInTenSeconds ;
176
+ }
177
+ this . _clearReconnectionTask ( ) ;
178
+ this . _refreshReconnectionTicket = setTimeout ( ( ) => {
179
+ this . _socket . emit ( 'refreshReconnectionTicket' , ( status , data ) => {
180
+ if ( status !== 'ok' ) {
181
+ Logger . warning ( 'Failed to refresh reconnection ticket.' ) ;
182
+ return ;
183
+ }
184
+ this . _onReconnectionTicket ( data ) ;
185
+ } ) ;
186
+ } , refreshAfter ) ;
187
+ }
188
+
189
+ _clearReconnectionTask ( ) {
190
+ clearTimeout ( this . _refreshReconnectionTicket ) ;
191
+ this . _refreshReconnectionTicket = null ;
192
+ }
152
193
}
0 commit comments