From 2f3769b33f964a145642d5752f6000ff582ff12d Mon Sep 17 00:00:00 2001 From: Gavin Llewellyn Date: Mon, 19 Aug 2013 17:46:02 +0100 Subject: [PATCH] Fix #148: WebSocket reconnection behaviour --- src/Transport.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Transport.js b/src/Transport.js index 0c5e2cda0..b45b89bd3 100644 --- a/src/Transport.js +++ b/src/Transport.js @@ -64,6 +64,16 @@ Transport.prototype = { console.log(LOG_PREFIX +'closing WebSocket ' + this.server.ws_uri); this.ws.close(); } + + if (this.reconnectTimer !== null) { + window.clearTimeout(this.reconnectTimer); + this.reconnectTimer = null; + this.ua.emit('disconnected', this.ua, { + transport: this, + code: this.lastTransportError.code, + reason: this.lastTransportError.reason + }); + } }, /** @@ -119,7 +129,12 @@ Transport.prototype = { console.log(LOG_PREFIX +'WebSocket ' + this.server.ws_uri + ' connected'); // Clear reconnectTimer since we are not disconnected - window.clearTimeout(this.reconnectTimer); + if (this.reconnectTimer !== null) { + window.clearTimeout(this.reconnectTimer); + this.reconnectTimer = null; + } + // Reset reconnection_attempts + this.reconnection_attempts = 0; // Disable closed this.closed = false; // Trigger onTransportConnected callback @@ -146,8 +161,6 @@ Transport.prototype = { this.ua.onTransportClosed(this); // Check whether the user requested to close. if(!this.closed) { - // Reset reconnection_attempts - this.reconnection_attempts = 0; this.reConnect(); } else { this.ua.emit('disconnected', this.ua, { @@ -261,7 +274,9 @@ Transport.prototype = { console.log(LOG_PREFIX +'trying to reconnect to WebSocket ' + this.server.ws_uri + ' (reconnection attempt ' + this.reconnection_attempts + ')'); this.reconnectTimer = window.setTimeout(function() { - transport.connect();}, this.ua.configuration.ws_server_reconnection_timeout * 1000); + transport.connect(); + transport.reconnectTimer = null; + }, this.ua.configuration.ws_server_reconnection_timeout * 1000); } } };