Repository navigation
Expand file tree
/
Copy pathws-client.js
More file actions
124 lines (107 loc) · 3.76 KB
/
Copy pathws-client.js
File metadata and controls
124 lines (107 loc) · 3.76 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// ws-client.js — WebSocket client factory: createWsClient(url, token)
// Per-instance connect/disconnect/send/subscribe with auto-reconnect
// (exponential backoff + jitter), a keepalive ping, and inbound {topic, data}
// frames published onto the shared channel bus. Zero dependencies.
//
// Channel topics published by every client:
// <topic> data of an inbound JSON frame { topic, data }
// ws:message every inbound frame (parsed JSON, or the raw string)
// ws:open null
// ws:close { code, reason }
// ws:error the error event
import { publish, subscribe as channelSubscribe } from './channels.js';
const INITIAL_DELAY = 1000;
const MAX_DELAY = 30000;
const HEARTBEAT_INTERVAL = 30000;
export function createWsClient(initialUrl = '', initialToken = '') {
let ws = null;
let reconnectDelay = INITIAL_DELAY;
let heartbeatTimer = null;
let reconnectTimer = null;
let url = initialUrl;
let token = initialToken;
// Full jitter in [delay/2, delay] so many clients do not reconnect in lockstep.
const jitter = (delay) => delay * (0.5 + Math.random() * 0.5);
function startHeartbeat() {
stopHeartbeat();
heartbeatTimer = setInterval(() => send({ type: 'ping' }), HEARTBEAT_INTERVAL);
}
function stopHeartbeat() {
clearInterval(heartbeatTimer);
heartbeatTimer = null;
}
function cancelReconnect() {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
function scheduleReconnect() {
if (reconnectTimer) return;
reconnectTimer = setTimeout(() => {
reconnectTimer = null;
open();
}, jitter(reconnectDelay));
reconnectDelay = Math.min(reconnectDelay * 2, MAX_DELAY);
}
// Close the current socket without letting its late events reach us.
function retire() {
if (!ws) return;
const old = ws;
ws = null;
old.onopen = old.onmessage = old.onerror = old.onclose = null;
try { old.close(); } catch { /* already closed */ }
}
function open() {
retire();
let wsUrl = url;
if (token) wsUrl += (url.includes('?') ? '&' : '?') + 'token=' + encodeURIComponent(token);
const socket = new WebSocket(wsUrl);
ws = socket;
socket.onopen = () => {
reconnectDelay = INITIAL_DELAY;
startHeartbeat();
publish('ws:open', null);
};
socket.onmessage = (event) => {
let msg;
try { msg = JSON.parse(event.data); } catch { msg = event.data; }
if (msg !== null && typeof msg === 'object' && msg.topic) publish(msg.topic, msg.data ?? msg);
publish('ws:message', msg);
};
socket.onerror = (err) => publish('ws:error', err);
socket.onclose = (event) => {
if (ws !== socket) return;
ws = null;
stopHeartbeat();
publish('ws:close', { code: event.code, reason: event.reason });
scheduleReconnect();
};
return socket;
}
/** (Re)connect. Arguments override the url/token given to createWsClient. */
function connect(connectUrl, connectToken) {
url = connectUrl ?? url;
token = connectToken ?? token;
cancelReconnect();
stopHeartbeat();
return open();
}
/** Close the socket and stop reconnecting. */
function disconnect() {
cancelReconnect();
stopHeartbeat();
retire();
}
/** Send a string or a JSON-serialisable value. Returns false if not open. */
function send(data) {
if (!ws || ws.readyState !== WebSocket.OPEN) return false;
ws.send(typeof data === 'string' ? data : JSON.stringify(data));
return true;
}
function subscribe(topic, handler, options) {
return channelSubscribe(topic, handler, options);
}
return { connect, disconnect, send, subscribe };
}
// A default client for apps that need only one connection.
const defaultClient = createWsClient();
export const { connect, disconnect, send, subscribe } = defaultClient;