Skip to content
Permalink
Browse files

Fix #148: WebSocket reconnection behaviour

  • Loading branch information
gavllew committed Aug 19, 2013
1 parent d141864 commit 2f3769b33f964a145642d5752f6000ff582ff12d
Showing with 19 additions and 4 deletions.
  1. +19 −4 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);
}
}
};

0 comments on commit 2f3769b

Please sign in to comment.
You can’t perform that action at this time.