@@ -91,7 +91,7 @@ export abstract class Signaling implements saltyrtc.Signaling {
91
91
}
92
92
this . handoverState . onBoth = ( ) => {
93
93
this . client . emit ( { type : 'handover' } ) ;
94
- this . ws . close ( CloseCode . Handover ) ;
94
+ this . closeWebsocket ( CloseCode . Handover ) ;
95
95
} ;
96
96
}
97
97
@@ -161,8 +161,10 @@ export abstract class Signaling implements saltyrtc.Signaling {
161
161
162
162
/**
163
163
* Disconnect from the signaling server.
164
+ *
165
+ * @param unbind Whether to unbind all WebSocket events.
164
166
*/
165
- public disconnect ( ) : void {
167
+ public disconnect ( unbind = false ) : void {
166
168
const reason = CloseCode . ClosingNormal ;
167
169
168
170
// Update state
@@ -173,12 +175,8 @@ export abstract class Signaling implements saltyrtc.Signaling {
173
175
this . sendClose ( reason ) ;
174
176
}
175
177
176
- // Close WebSocket instance
177
- if ( this . ws !== null ) {
178
- console . debug ( this . logTag , 'Disconnecting WebSocket' ) ;
179
- this . ws . close ( reason ) ;
180
- }
181
- this . ws = null ;
178
+ // Close WebSocket instance and unbind all events
179
+ this . closeWebsocket ( reason , undefined , unbind ) ;
182
180
183
181
// Close task connections
184
182
if ( this . task !== null ) {
@@ -191,7 +189,45 @@ export abstract class Signaling implements saltyrtc.Signaling {
191
189
}
192
190
193
191
/**
194
- * Return connection path for websocket.
192
+ * Close the WebSocket connection.
193
+ *
194
+ * @param code The close code.
195
+ * @param reason The close reason.
196
+ * @param unbind Whether to unbind all events. This will move the Signaling
197
+ * instance into the `closed` state.
198
+ */
199
+ private closeWebsocket ( code ?: number , reason ?: string , unbind = false ) : void {
200
+ if ( this . ws !== null ) {
201
+ // Drop internal close codes
202
+ // see: https://github.com/saltyrtc/saltyrtc-meta/issues/110
203
+ if ( code === undefined || code <= 3000 ) {
204
+ code = CloseCode . ClosingNormal ;
205
+ }
206
+
207
+ // Disconnect
208
+ console . debug ( this . logTag , `Disconnecting WebSocket, close code: ${ code } ` ) ;
209
+ this . ws . close ( code , reason ) ;
210
+
211
+ // Unbind events?
212
+ if ( unbind ) {
213
+ this . ws . removeEventListener ( 'open' , this . onOpen ) ;
214
+ this . ws . removeEventListener ( 'error' , this . onError ) ;
215
+ this . ws . removeEventListener ( 'close' , this . onClose ) ;
216
+ this . ws . removeEventListener ( 'message' , this . onMessage ) ;
217
+ }
218
+
219
+ // Forget instance
220
+ this . ws = null ;
221
+
222
+ // Move into closed state (if necessary)
223
+ if ( unbind ) {
224
+ this . setState ( 'closed' ) ;
225
+ }
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Return connection path for WebSocket.
195
231
*/
196
232
protected abstract getWebsocketPath ( ) : string ;
197
233
@@ -246,30 +282,6 @@ export abstract class Signaling implements saltyrtc.Signaling {
246
282
' (' + explainCloseCode ( ev . code ) + ')' ) ;
247
283
this . setState ( 'closed' ) ;
248
284
this . client . emit ( { type : 'connection-closed' , data : ev . code } ) ;
249
- const logError = ( reason : string ) => console . error ( this . logTag , 'Websocket close reason:' , reason ) ;
250
- switch ( ev . code ) {
251
- case CloseCode . GoingAway :
252
- logError ( 'Server is being shut down' ) ;
253
- break ;
254
- case CloseCode . NoSharedSubprotocol :
255
- logError ( 'No shared sub-protocol could be found' ) ;
256
- break ;
257
- case CloseCode . PathFull :
258
- logError ( 'Path full (no free responder byte)' ) ;
259
- break ;
260
- case CloseCode . ProtocolError :
261
- logError ( 'Protocol error' ) ;
262
- break ;
263
- case CloseCode . InternalError :
264
- logError ( 'Internal error' ) ;
265
- break ;
266
- case CloseCode . DroppedByInitiator :
267
- logError ( 'Dropped by initiator' ) ;
268
- break ;
269
- case CloseCode . InvalidKey :
270
- logError ( 'Invalid server key' ) ;
271
- break ;
272
- }
273
285
}
274
286
}
275
287
@@ -915,19 +927,11 @@ export abstract class Signaling implements saltyrtc.Signaling {
915
927
* - Set `this.ws` to `null`
916
928
* - Set `this.status` to `new`
917
929
* - Reset the server combined sequence
930
+ * - Unbind all events
918
931
*/
919
932
public resetConnection ( reason ?: number ) : void {
920
933
// Close WebSocket instance
921
- if ( this . ws !== null ) {
922
- console . debug ( this . logTag , 'Disconnecting WebSocket (close code ' + reason + ')' ) ;
923
- if ( reason === CloseCode . GoingAway ) {
924
- // See https://github.com/saltyrtc/saltyrtc-meta/pull/111/
925
- this . ws . close ( ) ;
926
- } else {
927
- this . ws . close ( reason ) ;
928
- }
929
- }
930
- this . ws = null ;
934
+ this . closeWebsocket ( reason , undefined , true ) ;
931
935
932
936
// Reset
933
937
this . server = new Server ( ) ;
0 commit comments