/
socket.js
61 lines (49 loc) · 1.54 KB
/
socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
/* global __webpack_dev_server_client__ */
/* eslint-disable
camelcase
*/
// this SockJSClient is here as a default fallback, in case inline mode
// is off or the client is not injected. This will be switched to
// WebsocketClient when it becomes the default
// important: the path to SockJSClient here is made to work in the 'client'
// directory, but is updated via the webpack compilation when compiled from
// the 'client-src' directory
const Client =
typeof __webpack_dev_server_client__ !== 'undefined'
? __webpack_dev_server_client__
: // eslint-disable-next-line import/no-unresolved
require('./clients/SockJSClient');
let retries = 0;
let client = null;
const socket = function initSocket(url, handlers) {
client = new Client(url);
client.onOpen(() => {
retries = 0;
});
client.onClose(() => {
if (retries === 0) {
handlers.close();
}
// Try to reconnect.
client = null;
// After 10 retries stop trying, to prevent logspam.
if (retries <= 10) {
// Exponentially increase timeout to reconnect.
// Respectfully copied from the package `got`.
// eslint-disable-next-line no-mixed-operators, no-restricted-properties
const retryInMs = 1000 * Math.pow(2, retries) + Math.random() * 100;
retries += 1;
setTimeout(() => {
socket(url, handlers);
}, retryInMs);
}
});
client.onMessage((data) => {
const msg = JSON.parse(data);
if (handlers[msg.type]) {
handlers[msg.type](msg.data);
}
});
};
module.exports = socket;