Skip to content

Commit 2f3769b

Browse files
author
Gavin Llewellyn
committed
Fix #148: WebSocket reconnection behaviour
1 parent d141864 commit 2f3769b

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/Transport.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ Transport.prototype = {
6464
console.log(LOG_PREFIX +'closing WebSocket ' + this.server.ws_uri);
6565
this.ws.close();
6666
}
67+
68+
if (this.reconnectTimer !== null) {
69+
window.clearTimeout(this.reconnectTimer);
70+
this.reconnectTimer = null;
71+
this.ua.emit('disconnected', this.ua, {
72+
transport: this,
73+
code: this.lastTransportError.code,
74+
reason: this.lastTransportError.reason
75+
});
76+
}
6777
},
6878

6979
/**
@@ -119,7 +129,12 @@ Transport.prototype = {
119129

120130
console.log(LOG_PREFIX +'WebSocket ' + this.server.ws_uri + ' connected');
121131
// Clear reconnectTimer since we are not disconnected
122-
window.clearTimeout(this.reconnectTimer);
132+
if (this.reconnectTimer !== null) {
133+
window.clearTimeout(this.reconnectTimer);
134+
this.reconnectTimer = null;
135+
}
136+
// Reset reconnection_attempts
137+
this.reconnection_attempts = 0;
123138
// Disable closed
124139
this.closed = false;
125140
// Trigger onTransportConnected callback
@@ -146,8 +161,6 @@ Transport.prototype = {
146161
this.ua.onTransportClosed(this);
147162
// Check whether the user requested to close.
148163
if(!this.closed) {
149-
// Reset reconnection_attempts
150-
this.reconnection_attempts = 0;
151164
this.reConnect();
152165
} else {
153166
this.ua.emit('disconnected', this.ua, {
@@ -261,7 +274,9 @@ Transport.prototype = {
261274
console.log(LOG_PREFIX +'trying to reconnect to WebSocket ' + this.server.ws_uri + ' (reconnection attempt ' + this.reconnection_attempts + ')');
262275

263276
this.reconnectTimer = window.setTimeout(function() {
264-
transport.connect();}, this.ua.configuration.ws_server_reconnection_timeout * 1000);
277+
transport.connect();
278+
transport.reconnectTimer = null;
279+
}, this.ua.configuration.ws_server_reconnection_timeout * 1000);
265280
}
266281
}
267282
};

0 commit comments

Comments
 (0)