Skip to content

Commit 2cc9f83

Browse files
committed
Reconnect on disconnect
1 parent 13892fb commit 2cc9f83

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

deck-remote-client/index.html

+19-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,25 @@
7171

7272
var remoteClient = {
7373
myWebSocket: null,
74+
ip: '127.0.0.1',
75+
port: '8080',
76+
reconnectAttempt: null,
77+
reconnect: function() {
78+
if (this.myWebSocket != null && this.myWebSocket.readyState <= WebSocket.OPEN) return;
79+
if (this.reconnectAttempt) return;
80+
var $this = this;
81+
this.reconnectAttempt = setTimeout(function() { $this.init() }, 1000);
82+
},
7483
init: function() {
75-
$this = this;
76-
this.myWebSocket = new WebSocket("ws://127.0.0.1:8080"),
84+
this.reconnectAttempt = null;
85+
86+
var $this = this;
87+
try {
88+
this.myWebSocket = new WebSocket("ws://" + this.ip + ":" + this.port);
89+
} catch (e) {
90+
this.reconnect();
91+
return;
92+
}
7793
this.myWebSocket.onopen = function(evt) {
7894
$this.myWebSocket.send('{"type":"identify", "data":"client"}');
7995
};
@@ -90,6 +106,7 @@
90106
}
91107
};
92108
this.myWebSocket.onclose = function(evt) {
109+
$this.reconnect();
93110
};
94111
},
95112
next: function() {

deck-remote/deck.remote.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,24 @@ the deck container.
2626
myWebSocket: null,
2727
ip: '127.0.0.1',
2828
port: '8080',
29+
reconnectAttempt: null,
30+
reconnect: function() {
31+
if (this.myWebSocket != null && this.myWebSocket.readyState <= WebSocket.OPEN) return;
32+
if (this.reconnectAttempt) return;
33+
var $this = this;
34+
this.reconnectAttempt = setTimeout(function() { $this.init() }, 1000);
35+
},
2936
init: function() {
37+
this.reconnectAttempt = null;
38+
3039
if ('WebSocket' in window == false && 'MozWebSocket' in window) window.WebSocket = window.MozWebSocket;
3140

32-
this.myWebSocket = new WebSocket("ws://" + this.ip + ":" + this.port);
41+
try {
42+
this.myWebSocket = new WebSocket("ws://" + this.ip + ":" + this.port);
43+
} catch (e) {
44+
this.reconnect();
45+
return;
46+
}
3347

3448
if (location.href.indexOf('#mirror') != -1) {
3549
this.initMirror(parseInt(location.href.substr(7+location.href.indexOf('#mirror'))));
@@ -38,6 +52,7 @@ the deck container.
3852
$this = this;
3953
this.myWebSocket.onopen = function(evt) {
4054
$this.myWebSocket.send('{"type":"identify", "data":"server", "url":"' + location.href + '"}');
55+
$this.sendStatus();
4156
};
4257
this.myWebSocket.onmessage = function(evt) {
4358
try {
@@ -49,7 +64,9 @@ the deck container.
4964
} catch (e) {
5065
}
5166
};
52-
this.myWebSocket.onclose = function(evt) {};
67+
this.myWebSocket.onclose = function(evt) {
68+
$this.reconnect();
69+
};
5370
},
5471
initMirror: function(offset) {
5572
$this = this;
@@ -65,7 +82,9 @@ the deck container.
6582
} catch (e) {
6683
}
6784
};
68-
this.myWebSocket.onclose = function(evt) {};
85+
this.myWebSocket.onclose = function(evt) {
86+
$this.reconnect();
87+
};
6988
this.sendStatus = function() {};
7089
},
7190
sendStatus: function() {

0 commit comments

Comments
 (0)