ソースを参照

Fix WebSocket reconnect on error/close; see #130

Dominic Szablewski 8 年 前
コミット
be5c0dd5e2
共有1 個のファイルを変更した7 個の追加2 個の削除を含む
  1. 7 2
      src/websocket.js

+ 7 - 2
src/websocket.js ファイルの表示

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