Bläddra i källkod

Fix WebSocket reconnect on error/close; see #130

Dominic Szablewski 9 år sedan
förälder
incheckning
be5c0dd5e2
1 ändrade filer med 7 tillägg och 2 borttagningar
  1. 7 2
      src/websocket.js

+ 7 - 2
src/websocket.js Visa fil

7
 	this.callbacks = {connect: [], data: []};
7
 	this.callbacks = {connect: [], data: []};
8
 	this.destination = null;
8
 	this.destination = null;
9
 
9
 
10
-	this.reconnectInterval = options.reconnectInterval || 5;
10
+	this.reconnectInterval = options.reconnectInterval !== undefined
11
+		? options.reconnectInterval
12
+		: 5;
11
 	this.shouldAttemptReconnect = !!this.reconnectInterval;
13
 	this.shouldAttemptReconnect = !!this.reconnectInterval;
12
 
14
 
13
 	this.completed = false;
15
 	this.completed = false;
14
 	this.established = false;
16
 	this.established = false;
15
 	this.progress = 0;
17
 	this.progress = 0;
18
+
19
+	this.reconnectTimeoutId = 0;
16
 };
20
 };
17
 
21
 
18
 WSSource.prototype.connect = function(destination) {
22
 WSSource.prototype.connect = function(destination) {
48
 
52
 
49
 WSSource.prototype.onClose = function() {
53
 WSSource.prototype.onClose = function() {
50
 	if (this.shouldAttemptReconnect) {
54
 	if (this.shouldAttemptReconnect) {
51
-		setTimeout(function(){
55
+		clearTimeout(this.reconnectTimeoutId);
56
+		this.reconnectTimeoutId = setTimeout(function(){
52
 			this.start();	
57
 			this.start();	
53
 		}.bind(this), this.reconnectInterval*1000);
58
 		}.bind(this), this.reconnectInterval*1000);
54
 	}
59
 	}