Skip to content

Commit

Permalink
Fix WebSocket reconnect on error/close; see phoboslab#130
Browse files Browse the repository at this point in the history
Don’t reconnect when reconnectInterval is 0. Cancel previous reconnect
timeout when installing a new one.
  • Loading branch information
phoboslab committed Feb 18, 2017
1 parent c032764 commit be5c0dd
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ var WSSource = function(url, options) {
this.callbacks = {connect: [], data: []};
this.destination = null;

this.reconnectInterval = options.reconnectInterval || 5;
this.reconnectInterval = options.reconnectInterval !== undefined
? options.reconnectInterval
: 5;
this.shouldAttemptReconnect = !!this.reconnectInterval;

this.completed = false;
this.established = false;
this.progress = 0;

this.reconnectTimeoutId = 0;
};

WSSource.prototype.connect = function(destination) {
Expand Down Expand Up @@ -48,7 +52,8 @@ WSSource.prototype.onOpen = function() {

WSSource.prototype.onClose = function() {
if (this.shouldAttemptReconnect) {
setTimeout(function(){
clearTimeout(this.reconnectTimeoutId);
this.reconnectTimeoutId = setTimeout(function(){
this.start();
}.bind(this), this.reconnectInterval*1000);
}
Expand Down

0 comments on commit be5c0dd

Please sign in to comment.