Skip to content

Commit 5914f2e

Browse files
authored
Limit websocket retries. (#589)
Before, the client script would retry for a websocket connection every two seconds. This caused massive log spam. Now, it will retry with an exponential timeout, and with a max of 10 retries.
1 parent 0b625b9 commit 5914f2e

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

client/index.js

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global __resourceQuery */
22
var url = require('url');
3-
var SockJS = require("sockjs-client");
43
var stripAnsi = require('strip-ansi');
4+
var socket = require('./socket');
55

66
function getCurrentScriptSource() {
77
// `document.currentScript` is the most accurate way to find the current script,
@@ -28,10 +28,8 @@ if(typeof __resourceQuery === "string" && __resourceQuery) {
2828
urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true);
2929
}
3030

31-
var sock = null;
3231
var hot = false;
3332
var initial = true;
34-
var connected = false;
3533
var currentHash = "";
3634
var logLevel = "info";
3735

@@ -84,40 +82,21 @@ var onSocketMsg = {
8482
for(var i = 0; i < errors.length; i++)
8583
log("error", stripAnsi(errors[i]));
8684
if(initial) return initial = false;
85+
},
86+
close: function() {
87+
log("error", "[WDS] Disconnected!");
8788
}
8889
};
8990

90-
var newConnection = function() {
91-
sock = new SockJS(url.format({
92-
protocol: (window.location.protocol === "https:" || urlParts.hostname === '0.0.0.0') ? window.location.protocol : urlParts.protocol,
93-
auth: urlParts.auth,
94-
hostname: (urlParts.hostname === '0.0.0.0') ? window.location.hostname : urlParts.hostname,
95-
port: (urlParts.port === '0') ? window.location.port : urlParts.port,
96-
pathname: urlParts.path == null || urlParts.path === '/' ? "/sockjs-node" : urlParts.path
97-
}));
98-
99-
sock.onclose = function() {
100-
if(connected)
101-
log("error", "[WDS] Disconnected!");
102-
103-
connected = false;
104-
105-
// Try to reconnect.
106-
sock = null;
107-
setTimeout(function() {
108-
newConnection();
109-
}, 2000);
110-
};
111-
112-
sock.onmessage = function(e) {
113-
connected = true;
114-
// This assumes that all data sent via the websocket is JSON.
115-
var msg = JSON.parse(e.data);
116-
onSocketMsg[msg.type](msg.data);
117-
};
118-
};
91+
var socketUrl = url.format({
92+
protocol: (window.location.protocol === "https:" || urlParts.hostname === '0.0.0.0') ? window.location.protocol : urlParts.protocol,
93+
auth: urlParts.auth,
94+
hostname: (urlParts.hostname === '0.0.0.0') ? window.location.hostname : urlParts.hostname,
95+
port: (urlParts.port === '0') ? window.location.port : urlParts.port,
96+
pathname: urlParts.path == null || urlParts.path === '/' ? "/sockjs-node" : urlParts.path
97+
});
11998

120-
newConnection();
99+
socket(socketUrl, onSocketMsg);
121100

122101
function reloadApp() {
123102
if(hot) {

client/live.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
var $ = require("jquery");
2-
var SockJS = require("sockjs-client");
32
var stripAnsi = require('strip-ansi');
3+
var socket = require('./socket');
44
require("./style.css");
55

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

10-
var newConnection = function(handlers) {
11-
sock = new SockJS('/sockjs-node');
12-
13-
sock.onclose = function() {
14-
handlers.close();
15-
16-
// Try to reconnect.
17-
sock = null;
18-
setTimeout(function() {
19-
newConnection(handlers);
20-
}, 2000);
21-
};
22-
23-
sock.onmessage = function(e) {
24-
// This assumes that all data sent via the websocket is JSON.
25-
var msg = JSON.parse(e.data);
26-
handlers[msg.type](msg.data);
27-
};
28-
};
29-
309
$(function() {
3110
$("body").html(require("./page.pug")());
3211
var status = $("#status");
@@ -112,7 +91,7 @@ $(function() {
11291
}
11392
};
11493

115-
newConnection(onSocketMsg);
94+
socket("/sockjs-node", onSocketMsg);
11695

11796
iframe.load(function() {
11897
status.text("App ready.");

client/socket.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var SockJS = require("sockjs-client");
2+
3+
var retries = 0;
4+
var sock = null;
5+
6+
function socket(url, handlers) {
7+
sock = new SockJS(url);
8+
9+
sock.onopen = function() {
10+
retries = 0;
11+
}
12+
13+
sock.onclose = function() {
14+
if(retries === 0)
15+
handlers.close();
16+
17+
// Try to reconnect.
18+
sock = null;
19+
20+
// After 10 retries stop trying, to prevent logspam.
21+
if(retries <= 10) {
22+
// Exponentially increase timeout to reconnect.
23+
// Respectfully copied from the package `got`.
24+
var retryInMs = 1000 * Math.pow(2, retries) + Math.random() * 100;
25+
retries += 1;
26+
27+
setTimeout(function() {
28+
socket(url, handlers);
29+
}, retryInMs);
30+
}
31+
};
32+
33+
sock.onmessage = function(e) {
34+
// This assumes that all data sent via the websocket is JSON.
35+
var msg = JSON.parse(e.data);
36+
if(handlers[msg.type])
37+
handlers[msg.type](msg.data);
38+
};
39+
}
40+
41+
module.exports = socket;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
],
5151
"scripts": {
5252
"prepublish": "webpack ./client/live.js client/live.bundle.js --color --config client/webpack.config.js -p && webpack ./client/index.js client/index.bundle.js --color --config client/webpack.config.js -p",
53-
"lint": "eslint bin lib test examples client/{index,live,webpack.config}.js",
53+
"lint": "eslint bin lib test examples client/{index,live,socket,webpack.config}.js",
5454
"beautify": "npm run lint -- --fix",
5555
"travis": "npm run lint && node lib/Server.js"
5656
}

0 commit comments

Comments
 (0)