@iam4x/reconnecting-websocket 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +33 -0
- package/dist/index.js +110 -2
- package/package.json +6 -2
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,34 @@
|
|
|
1
|
+
type EventType = "open" | "message" | "close" | "reconnect" | "error";
|
|
2
|
+
type Listener = (payload: any) => void;
|
|
3
|
+
interface ReconnectOptions {
|
|
4
|
+
retryDelay?: number;
|
|
5
|
+
maxRetryDelay?: number;
|
|
6
|
+
connectionTimeout?: number;
|
|
7
|
+
backoffFactor?: number;
|
|
8
|
+
WebSocketConstructor?: typeof WebSocket;
|
|
9
|
+
}
|
|
10
|
+
export declare class ReconnectingWebSocket {
|
|
11
|
+
options: Required<ReconnectOptions & {
|
|
12
|
+
url: string;
|
|
13
|
+
}>;
|
|
14
|
+
ws?: WebSocket;
|
|
15
|
+
abortController?: AbortController;
|
|
16
|
+
connectTimeout?: ReturnType<typeof setTimeout>;
|
|
17
|
+
reconnectTimeout?: ReturnType<typeof setTimeout>;
|
|
18
|
+
retryCount: number;
|
|
19
|
+
forcedClose: boolean;
|
|
20
|
+
wasConnected: boolean;
|
|
21
|
+
listeners: Record<EventType, Listener[]>;
|
|
22
|
+
get readyState(): number;
|
|
23
|
+
get bufferedAmount(): number;
|
|
24
|
+
constructor(url: string, options?: ReconnectOptions);
|
|
25
|
+
connect(): void;
|
|
26
|
+
emit(event: EventType, payload: any): void;
|
|
27
|
+
scheduleReconnect(): void;
|
|
28
|
+
clearTimers(): void;
|
|
29
|
+
addEventListener(event: EventType, listener: Listener): void;
|
|
30
|
+
removeEventListener(event: EventType, listener: Listener): void;
|
|
31
|
+
send(...args: Parameters<WebSocket["send"]>): void;
|
|
32
|
+
close(...args: Parameters<WebSocket["close"]>): void;
|
|
33
|
+
}
|
|
1
34
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,110 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export class ReconnectingWebSocket {
|
|
2
|
+
options;
|
|
3
|
+
ws;
|
|
4
|
+
abortController;
|
|
5
|
+
connectTimeout;
|
|
6
|
+
reconnectTimeout;
|
|
7
|
+
retryCount = 0;
|
|
8
|
+
forcedClose = false;
|
|
9
|
+
wasConnected = false;
|
|
10
|
+
listeners = {
|
|
11
|
+
open: [],
|
|
12
|
+
message: [],
|
|
13
|
+
close: [],
|
|
14
|
+
reconnect: [],
|
|
15
|
+
error: [],
|
|
16
|
+
};
|
|
17
|
+
get readyState() {
|
|
18
|
+
return this.ws?.readyState ?? WebSocket.CLOSED;
|
|
19
|
+
}
|
|
20
|
+
get bufferedAmount() {
|
|
21
|
+
return this.ws?.bufferedAmount ?? 0;
|
|
22
|
+
}
|
|
23
|
+
constructor(url, options = {}) {
|
|
24
|
+
this.options = {
|
|
25
|
+
url,
|
|
26
|
+
retryDelay: options.retryDelay ?? 1000,
|
|
27
|
+
maxRetryDelay: options.maxRetryDelay ?? 30_000,
|
|
28
|
+
connectionTimeout: options.connectionTimeout ?? 10_000,
|
|
29
|
+
backoffFactor: options.backoffFactor ?? 2,
|
|
30
|
+
WebSocketConstructor: options.WebSocketConstructor ?? WebSocket,
|
|
31
|
+
};
|
|
32
|
+
this.connect();
|
|
33
|
+
}
|
|
34
|
+
connect() {
|
|
35
|
+
this.abortController = new AbortController();
|
|
36
|
+
this.abortController.signal.addEventListener("abort", () => {
|
|
37
|
+
if (this.ws?.readyState === WebSocket.CONNECTING) {
|
|
38
|
+
this.ws.close();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
this.ws = new this.options.WebSocketConstructor(this.options.url);
|
|
42
|
+
this.connectTimeout = setTimeout(() => {
|
|
43
|
+
this.abortController?.abort();
|
|
44
|
+
}, this.options.connectionTimeout);
|
|
45
|
+
this.ws.addEventListener("open", (event) => {
|
|
46
|
+
this.clearTimers();
|
|
47
|
+
this.retryCount = 0;
|
|
48
|
+
this.emit("open", event);
|
|
49
|
+
// Emit reconnect event if this was a reconnection after a disconnection
|
|
50
|
+
if (this.wasConnected) {
|
|
51
|
+
this.emit("reconnect", event);
|
|
52
|
+
}
|
|
53
|
+
this.wasConnected = true;
|
|
54
|
+
});
|
|
55
|
+
this.ws.addEventListener("message", (event) => {
|
|
56
|
+
this.emit("message", event);
|
|
57
|
+
});
|
|
58
|
+
this.ws.addEventListener("close", (event) => {
|
|
59
|
+
this.emit("close", { code: event.code, reason: event.reason });
|
|
60
|
+
if (!this.forcedClose) {
|
|
61
|
+
this.scheduleReconnect();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
this.ws.addEventListener("error", (event) => {
|
|
65
|
+
this.emit("error", event);
|
|
66
|
+
// Error will typically cause connection to close, triggering reconnect
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
emit(event, payload) {
|
|
70
|
+
for (const listener of this.listeners[event]) {
|
|
71
|
+
listener(payload);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
scheduleReconnect() {
|
|
75
|
+
const { retryDelay, backoffFactor, maxRetryDelay } = this.options;
|
|
76
|
+
const delay = Math.min(retryDelay * Math.pow(backoffFactor, this.retryCount), maxRetryDelay);
|
|
77
|
+
this.retryCount += 1;
|
|
78
|
+
this.reconnectTimeout = setTimeout(() => this.connect(), delay);
|
|
79
|
+
}
|
|
80
|
+
clearTimers() {
|
|
81
|
+
if (this.connectTimeout) {
|
|
82
|
+
clearTimeout(this.connectTimeout);
|
|
83
|
+
this.connectTimeout = undefined;
|
|
84
|
+
}
|
|
85
|
+
if (this.reconnectTimeout) {
|
|
86
|
+
clearTimeout(this.reconnectTimeout);
|
|
87
|
+
this.reconnectTimeout = undefined;
|
|
88
|
+
}
|
|
89
|
+
if (this.abortController) {
|
|
90
|
+
this.abortController = undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
addEventListener(event, listener) {
|
|
94
|
+
this.listeners[event].push(listener);
|
|
95
|
+
}
|
|
96
|
+
removeEventListener(event, listener) {
|
|
97
|
+
this.listeners[event] = this.listeners[event].filter((l) => l !== listener);
|
|
98
|
+
}
|
|
99
|
+
send(...args) {
|
|
100
|
+
this.ws?.send(...args);
|
|
101
|
+
}
|
|
102
|
+
close(...args) {
|
|
103
|
+
this.forcedClose = true;
|
|
104
|
+
this.clearTimers();
|
|
105
|
+
if (this.ws) {
|
|
106
|
+
this.ws.close(...args);
|
|
107
|
+
this.ws = undefined;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
package/package.json
CHANGED
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
"module": "src/index.ts",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.1",
|
|
7
7
|
"private": false,
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
8
9
|
"exports": {
|
|
9
|
-
".":
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
10
14
|
},
|
|
11
15
|
"files": [
|
|
12
16
|
"dist"
|