-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
40 lines (32 loc) · 1022 Bytes
/
client.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
/*global WebSocket */
(() => {
const port = 8888,
retryTime = 10 * 60 * 1000;
const log = (...things) => console.log("Refresh client:", ...things);
const RefreshSocket = class {
constructor (address = `ws://${window.location.hostname}:${port}`) {
this.address = address;
this.init();
}
init () {
let refreshSocket = new WebSocket(this.address, "refresh-protocol");
log(`Trying to open connection to ${this.address}…`);
refreshSocket.addEventListener(
"open",
() => log(`Refresh socket to ${this.address} opened successfully`));
refreshSocket.addEventListener(
"message",
() => {
refreshSocket.close();
window.location.reload(true);
});
refreshSocket.addEventListener(
"close",
() => {
log(`Refresh socket to ${this.address} closed`);
window.setTimeout(() => this.init(), retryTime);
});
}
};
let socket = new RefreshSocket();
})();