Skip to content

Commit 1bc9212

Browse files
committed
Use timeout instead of interval for reconnecting socket.
1 parent 7dcd41d commit 1bc9212

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

client/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ var urlParts = url.parse(typeof __resourceQuery === "string" && __resourceQuery
88
scriptElements[scriptElements.length-1].getAttribute("src").replace(/\/[^\/]+$/, "")
99
);
1010

11-
var recInterval = null;
1211
var sock = null;
12+
var hot = false;
13+
var initial = true;
14+
var currentHash = "";
1315

1416
var newConnection = function() {
1517
sock = new SockJS(url.format({
@@ -20,14 +22,12 @@ var newConnection = function() {
2022
pathname: urlParts.path === '/' ? "/sockjs-node" : urlParts.path
2123
}));
2224

23-
clearInterval(recInterval);
24-
2525
sock.onclose = function() {
2626
console.error("[WDS] Disconnected!");
2727

2828
// Try to reconnect.
2929
sock = null;
30-
recInterval = setInterval(function () {
30+
setTimeout(function () {
3131
newConnection();
3232
}, 2000);
3333
};
@@ -41,10 +41,6 @@ var newConnection = function() {
4141

4242
newConnection();
4343

44-
var hot = false;
45-
var initial = true;
46-
var currentHash = "";
47-
4844
function reloadApp() {
4945
if(hot) {
5046
console.log("[WDS] App hot update...");

client/live.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@ var SockJS = require("sockjs-client");
33
var stripAnsi = require('strip-ansi');
44
require("./style.css");
55

6-
var recInterval = null;
76
var sock = null;
87
var hot = false;
98
var currentHash = "";
109

1110
var newConnection = function(handlers) {
1211
sock = new SockJS('/sockjs-node');
1312

14-
clearInterval(recInterval);
15-
1613
sock.onclose = function() {
1714
handlers.close();
1815

1916
// Try to reconnect.
2017
sock = null;
21-
recInterval = setInterval(function () {
18+
setTimeout(function () {
2219
newConnection(handlers);
2320
}, 2000);
2421
};

0 commit comments

Comments
 (0)