websocket-ts 1.1.1 → 2.1.2
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/LICENSE +1 -1
- package/README.md +157 -93
- package/dist/cjs/src/backoff/backoff.d.ts +24 -0
- package/dist/cjs/src/backoff/backoff.d.ts.map +1 -0
- package/{lib → dist/cjs/src}/backoff/backoff.js.map +1 -1
- package/dist/cjs/src/backoff/constantbackoff.d.ts +18 -0
- package/dist/cjs/src/backoff/constantbackoff.d.ts.map +1 -0
- package/dist/cjs/src/backoff/constantbackoff.js +43 -0
- package/dist/cjs/src/backoff/constantbackoff.js.map +1 -0
- package/dist/cjs/src/backoff/exponentialbackoff.d.ts +48 -0
- package/dist/cjs/src/backoff/exponentialbackoff.d.ts.map +1 -0
- package/dist/cjs/src/backoff/exponentialbackoff.js +81 -0
- package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -0
- package/dist/cjs/src/backoff/linearbackoff.d.ts +51 -0
- package/dist/cjs/src/backoff/linearbackoff.d.ts.map +1 -0
- package/dist/cjs/src/backoff/linearbackoff.js +89 -0
- package/dist/cjs/src/backoff/linearbackoff.js.map +1 -0
- package/dist/cjs/src/index.d.ts +14 -0
- package/dist/cjs/src/index.d.ts.map +1 -0
- package/dist/cjs/src/index.js +20 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/queue/array_queue.d.ts +17 -0
- package/dist/cjs/src/queue/array_queue.d.ts.map +1 -0
- package/dist/cjs/src/queue/array_queue.js +36 -0
- package/dist/cjs/src/queue/array_queue.js.map +1 -0
- package/dist/cjs/src/queue/queue.d.ts +41 -0
- package/dist/cjs/src/queue/queue.d.ts.map +1 -0
- package/{lib/buffer/buffer.js → dist/cjs/src/queue/queue.js} +1 -1
- package/dist/cjs/src/queue/queue.js.map +1 -0
- package/dist/cjs/src/queue/ring_queue.d.ts +20 -0
- package/dist/cjs/src/queue/ring_queue.d.ts.map +1 -0
- package/dist/cjs/src/queue/ring_queue.js +57 -0
- package/dist/cjs/src/queue/ring_queue.js.map +1 -0
- package/dist/cjs/src/websocket.d.ts +206 -0
- package/dist/cjs/src/websocket.d.ts.map +1 -0
- package/dist/cjs/src/websocket.js +434 -0
- package/dist/cjs/src/websocket.js.map +1 -0
- package/dist/cjs/src/websocket_buffer.d.ts +16 -0
- package/dist/cjs/src/websocket_buffer.d.ts.map +1 -0
- package/dist/cjs/src/websocket_buffer.js +3 -0
- package/dist/cjs/src/websocket_buffer.js.map +1 -0
- package/dist/cjs/src/websocket_builder.d.ts +147 -0
- package/dist/cjs/src/websocket_builder.d.ts.map +1 -0
- package/dist/cjs/src/websocket_builder.js +263 -0
- package/dist/cjs/src/websocket_builder.js.map +1 -0
- package/dist/cjs/src/websocket_event.d.ts +67 -0
- package/dist/cjs/src/websocket_event.d.ts.map +1 -0
- package/dist/cjs/src/websocket_event.js +22 -0
- package/dist/cjs/src/websocket_event.js.map +1 -0
- package/dist/cjs/src/websocket_options.d.ts +21 -0
- package/dist/cjs/src/websocket_options.d.ts.map +1 -0
- package/dist/cjs/src/websocket_options.js +3 -0
- package/dist/cjs/src/websocket_options.js.map +1 -0
- package/dist/cjs/src/websocket_retry_options.d.ts +20 -0
- package/dist/cjs/src/websocket_retry_options.d.ts.map +1 -0
- package/dist/cjs/src/websocket_retry_options.js +3 -0
- package/dist/cjs/src/websocket_retry_options.js.map +1 -0
- package/dist/esm/src/backoff/backoff.d.ts +24 -0
- package/dist/esm/src/backoff/backoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/backoff.js +2 -0
- package/dist/esm/src/backoff/backoff.js.map +1 -0
- package/dist/esm/src/backoff/constantbackoff.d.ts +18 -0
- package/dist/esm/src/backoff/constantbackoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/constantbackoff.js +30 -0
- package/dist/esm/src/backoff/constantbackoff.js.map +1 -0
- package/dist/esm/src/backoff/exponentialbackoff.d.ts +48 -0
- package/dist/esm/src/backoff/exponentialbackoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/exponentialbackoff.js +68 -0
- package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -0
- package/dist/esm/src/backoff/linearbackoff.d.ts +51 -0
- package/dist/esm/src/backoff/linearbackoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/linearbackoff.js +76 -0
- package/dist/esm/src/backoff/linearbackoff.js.map +1 -0
- package/dist/esm/src/index.d.ts +14 -0
- package/dist/esm/src/index.d.ts.map +1 -0
- package/dist/esm/src/index.js +9 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/queue/array_queue.d.ts +17 -0
- package/dist/esm/src/queue/array_queue.d.ts.map +1 -0
- package/dist/esm/src/queue/array_queue.js +31 -0
- package/dist/esm/src/queue/array_queue.js.map +1 -0
- package/dist/esm/src/queue/queue.d.ts +41 -0
- package/dist/esm/src/queue/queue.d.ts.map +1 -0
- package/dist/esm/src/queue/queue.js +2 -0
- package/dist/esm/src/queue/queue.js.map +1 -0
- package/dist/esm/src/queue/ring_queue.d.ts +20 -0
- package/dist/esm/src/queue/ring_queue.d.ts.map +1 -0
- package/dist/esm/src/queue/ring_queue.js +52 -0
- package/dist/esm/src/queue/ring_queue.js.map +1 -0
- package/dist/esm/src/websocket.d.ts +206 -0
- package/dist/esm/src/websocket.d.ts.map +1 -0
- package/dist/esm/src/websocket.js +356 -0
- package/dist/esm/src/websocket.js.map +1 -0
- package/dist/esm/src/websocket_buffer.d.ts +16 -0
- package/dist/esm/src/websocket_buffer.d.ts.map +1 -0
- package/dist/esm/src/websocket_buffer.js +2 -0
- package/dist/esm/src/websocket_buffer.js.map +1 -0
- package/dist/esm/src/websocket_builder.d.ts +147 -0
- package/dist/esm/src/websocket_builder.d.ts.map +1 -0
- package/dist/esm/src/websocket_builder.js +213 -0
- package/dist/esm/src/websocket_builder.js.map +1 -0
- package/dist/esm/src/websocket_event.d.ts +67 -0
- package/dist/esm/src/websocket_event.d.ts.map +1 -0
- package/dist/esm/src/websocket_event.js +19 -0
- package/dist/esm/src/websocket_event.js.map +1 -0
- package/dist/esm/src/websocket_options.d.ts +21 -0
- package/dist/esm/src/websocket_options.d.ts.map +1 -0
- package/dist/esm/src/websocket_options.js +2 -0
- package/dist/esm/src/websocket_options.js.map +1 -0
- package/dist/esm/src/websocket_retry_options.d.ts +20 -0
- package/dist/esm/src/websocket_retry_options.d.ts.map +1 -0
- package/dist/esm/src/websocket_retry_options.js +2 -0
- package/dist/esm/src/websocket_retry_options.js.map +1 -0
- package/package.json +27 -14
- package/src/backoff/backoff.ts +21 -13
- package/src/backoff/constantbackoff.ts +29 -11
- package/src/backoff/exponentialbackoff.ts +68 -28
- package/src/backoff/linearbackoff.ts +77 -26
- package/src/index.ts +23 -9
- package/src/queue/array_queue.ts +41 -0
- package/src/queue/queue.ts +46 -0
- package/src/queue/ring_queue.ts +69 -0
- package/src/websocket.ts +463 -138
- package/src/websocket_buffer.ts +18 -0
- package/src/websocket_builder.ts +274 -0
- package/src/websocket_event.ts +88 -0
- package/src/websocket_options.ts +23 -0
- package/src/websocket_retry_options.ts +22 -0
- package/{tsconfig.json → tsconfig.cjs.json} +8 -8
- package/tsconfig.esm.json +71 -0
- package/jest.config.js +0 -9
- package/lib/backoff/backoff.d.ts +0 -18
- package/lib/backoff/backoff.d.ts.map +0 -1
- package/lib/backoff/constantbackoff.d.ts +0 -11
- package/lib/backoff/constantbackoff.d.ts.map +0 -1
- package/lib/backoff/constantbackoff.js +0 -20
- package/lib/backoff/constantbackoff.js.map +0 -1
- package/lib/backoff/exponentialbackoff.d.ts +0 -22
- package/lib/backoff/exponentialbackoff.d.ts.map +0 -1
- package/lib/backoff/exponentialbackoff.js +0 -35
- package/lib/backoff/exponentialbackoff.js.map +0 -1
- package/lib/backoff/linearbackoff.d.ts +0 -19
- package/lib/backoff/linearbackoff.d.ts.map +0 -1
- package/lib/backoff/linearbackoff.js +0 -34
- package/lib/backoff/linearbackoff.js.map +0 -1
- package/lib/buffer/buffer.d.ts +0 -41
- package/lib/buffer/buffer.d.ts.map +0 -1
- package/lib/buffer/buffer.js.map +0 -1
- package/lib/buffer/lrubuffer.d.ts +0 -24
- package/lib/buffer/lrubuffer.d.ts.map +0 -1
- package/lib/buffer/lrubuffer.js +0 -76
- package/lib/buffer/lrubuffer.js.map +0 -1
- package/lib/buffer/timebuffer.d.ts +0 -25
- package/lib/buffer/timebuffer.d.ts.map +0 -1
- package/lib/buffer/timebuffer.js +0 -92
- package/lib/buffer/timebuffer.js.map +0 -1
- package/lib/index.d.ts +0 -10
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -22
- package/lib/index.js.map +0 -1
- package/lib/websocket.d.ts +0 -47
- package/lib/websocket.d.ts.map +0 -1
- package/lib/websocket.js +0 -120
- package/lib/websocket.js.map +0 -1
- package/lib/websocketBuilder.d.ts +0 -32
- package/lib/websocketBuilder.d.ts.map +0 -1
- package/lib/websocketBuilder.js +0 -68
- package/lib/websocketBuilder.js.map +0 -1
- package/src/buffer/buffer.ts +0 -45
- package/src/buffer/lrubuffer.ts +0 -80
- package/src/buffer/timebuffer.ts +0 -105
- package/src/websocketBuilder.ts +0 -98
- package/test/backoff/constantbackoff.test.ts +0 -21
- package/test/backoff/exponentialbackoff.test.ts +0 -13
- package/test/backoff/linearbackoff.test.ts +0 -32
- package/test/buffer/common.ts +0 -10
- package/test/buffer/lrubuffer.test.ts +0 -128
- package/test/buffer/timebuffer.test.ts +0 -115
- package/test/websocket.test.ts +0 -341
- package/test/websocketBuilder.test.ts +0 -36
- /package/{lib → dist/cjs/src}/backoff/backoff.js +0 -0
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { WebsocketEvent, } from "./websocket_event";
|
|
2
|
+
/**
|
|
3
|
+
* A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.
|
|
4
|
+
*/
|
|
5
|
+
export class Websocket {
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new websocket.
|
|
8
|
+
*
|
|
9
|
+
* @param url to connect to.
|
|
10
|
+
* @param protocols optional protocols to use.
|
|
11
|
+
* @param options optional options to use.
|
|
12
|
+
*/
|
|
13
|
+
constructor(url, protocols, options) {
|
|
14
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
|
|
15
|
+
this._closedByUser = false; // whether the websocket was closed by the user
|
|
16
|
+
/**
|
|
17
|
+
* Handles the 'open' event of the browser-native websocket.
|
|
18
|
+
* @param event to handle.
|
|
19
|
+
*/
|
|
20
|
+
this.handleOpenEvent = (event) => this.handleEvent(WebsocketEvent.open, event);
|
|
21
|
+
/**
|
|
22
|
+
* Handles the 'error' event of the browser-native websocket.
|
|
23
|
+
* @param event to handle.
|
|
24
|
+
*/
|
|
25
|
+
this.handleErrorEvent = (event) => this.handleEvent(WebsocketEvent.error, event);
|
|
26
|
+
/**
|
|
27
|
+
* Handles the 'close' event of the browser-native websocket.
|
|
28
|
+
* @param event to handle.
|
|
29
|
+
*/
|
|
30
|
+
this.handleCloseEvent = (event) => this.handleEvent(WebsocketEvent.close, event);
|
|
31
|
+
/**
|
|
32
|
+
* Handles the 'message' event of the browser-native websocket.
|
|
33
|
+
* @param event to handle.
|
|
34
|
+
*/
|
|
35
|
+
this.handleMessageEvent = (event) => this.handleEvent(WebsocketEvent.message, event);
|
|
36
|
+
this._url = url;
|
|
37
|
+
this._protocols = protocols;
|
|
38
|
+
// make a copy of the options to prevent the user from changing them
|
|
39
|
+
this._options = {
|
|
40
|
+
buffer: options === null || options === void 0 ? void 0 : options.buffer,
|
|
41
|
+
retry: {
|
|
42
|
+
maxRetries: (_a = options === null || options === void 0 ? void 0 : options.retry) === null || _a === void 0 ? void 0 : _a.maxRetries,
|
|
43
|
+
instantReconnect: (_b = options === null || options === void 0 ? void 0 : options.retry) === null || _b === void 0 ? void 0 : _b.instantReconnect,
|
|
44
|
+
backoff: (_c = options === null || options === void 0 ? void 0 : options.retry) === null || _c === void 0 ? void 0 : _c.backoff,
|
|
45
|
+
},
|
|
46
|
+
listeners: {
|
|
47
|
+
open: [...((_e = (_d = options === null || options === void 0 ? void 0 : options.listeners) === null || _d === void 0 ? void 0 : _d.open) !== null && _e !== void 0 ? _e : [])],
|
|
48
|
+
close: [...((_g = (_f = options === null || options === void 0 ? void 0 : options.listeners) === null || _f === void 0 ? void 0 : _f.close) !== null && _g !== void 0 ? _g : [])],
|
|
49
|
+
error: [...((_j = (_h = options === null || options === void 0 ? void 0 : options.listeners) === null || _h === void 0 ? void 0 : _h.error) !== null && _j !== void 0 ? _j : [])],
|
|
50
|
+
message: [...((_l = (_k = options === null || options === void 0 ? void 0 : options.listeners) === null || _k === void 0 ? void 0 : _k.message) !== null && _l !== void 0 ? _l : [])],
|
|
51
|
+
retry: [...((_o = (_m = options === null || options === void 0 ? void 0 : options.listeners) === null || _m === void 0 ? void 0 : _m.retry) !== null && _o !== void 0 ? _o : [])],
|
|
52
|
+
reconnect: [...((_q = (_p = options === null || options === void 0 ? void 0 : options.listeners) === null || _p === void 0 ? void 0 : _p.reconnect) !== null && _q !== void 0 ? _q : [])],
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
this._underlyingWebsocket = this.tryConnect();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Getter for the url.
|
|
59
|
+
*
|
|
60
|
+
* @return the url.
|
|
61
|
+
*/
|
|
62
|
+
get url() {
|
|
63
|
+
return this._url;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Getter for the protocols.
|
|
67
|
+
*
|
|
68
|
+
* @return the protocols, or undefined if none were provided.
|
|
69
|
+
*/
|
|
70
|
+
get protocols() {
|
|
71
|
+
return this._protocols;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Getter for the buffer.
|
|
75
|
+
*
|
|
76
|
+
* @return the buffer, or undefined if none was provided.
|
|
77
|
+
*/
|
|
78
|
+
get buffer() {
|
|
79
|
+
return this._options.buffer;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Getter for the maxRetries.
|
|
83
|
+
*
|
|
84
|
+
* @return the maxRetries, or undefined if none was provided (no limit).
|
|
85
|
+
*/
|
|
86
|
+
get maxRetries() {
|
|
87
|
+
return this._options.retry.maxRetries;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Getter for the instantReconnect.
|
|
91
|
+
*
|
|
92
|
+
* @return the instantReconnect, or undefined if none was provided.
|
|
93
|
+
*/
|
|
94
|
+
get instantReconnect() {
|
|
95
|
+
return this._options.retry.instantReconnect;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Getter for the backoff.
|
|
99
|
+
*
|
|
100
|
+
* @return the backoff, or undefined if none was provided.
|
|
101
|
+
*/
|
|
102
|
+
get backoff() {
|
|
103
|
+
return this._options.retry.backoff;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Whether the websocket was closed by the user. A websocket is closed by the user if the close().
|
|
107
|
+
*
|
|
108
|
+
* @return true if the websocket was closed by the user, false otherwise.
|
|
109
|
+
*/
|
|
110
|
+
get closedByUser() {
|
|
111
|
+
return this._closedByUser;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Getter for the last 'open' event, e.g. the last time the websocket was connected.
|
|
115
|
+
*
|
|
116
|
+
* @return the last 'open' event, or undefined if the websocket was never connected.
|
|
117
|
+
*/
|
|
118
|
+
get lastConnection() {
|
|
119
|
+
return this._lastConnection;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Getter for the underlying websocket. This can be used to access the browser's native websocket directly.
|
|
123
|
+
*
|
|
124
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
|
|
125
|
+
* @return the underlying websocket.
|
|
126
|
+
*/
|
|
127
|
+
get underlyingWebsocket() {
|
|
128
|
+
return this._underlyingWebsocket;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Getter for the readyState of the underlying websocket.
|
|
132
|
+
*
|
|
133
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
|
|
134
|
+
* @return the readyState of the underlying websocket.
|
|
135
|
+
*/
|
|
136
|
+
get readyState() {
|
|
137
|
+
return this._underlyingWebsocket.readyState;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Getter for the bufferedAmount of the underlying websocket.
|
|
141
|
+
*
|
|
142
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount
|
|
143
|
+
* @return the bufferedAmount of the underlying websocket.
|
|
144
|
+
*/
|
|
145
|
+
get bufferedAmount() {
|
|
146
|
+
return this._underlyingWebsocket.bufferedAmount;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Getter for the extensions of the underlying websocket.
|
|
150
|
+
*
|
|
151
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/extensions
|
|
152
|
+
* @return the extensions of the underlying websocket.
|
|
153
|
+
*/
|
|
154
|
+
get extensions() {
|
|
155
|
+
return this._underlyingWebsocket.extensions;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Getter for the binaryType of the underlying websocket.
|
|
159
|
+
*
|
|
160
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType
|
|
161
|
+
* @return the binaryType of the underlying websocket.
|
|
162
|
+
*/
|
|
163
|
+
get binaryType() {
|
|
164
|
+
return this._underlyingWebsocket.binaryType;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Setter for the binaryType of the underlying websocket.
|
|
168
|
+
*
|
|
169
|
+
* @param value to set, 'blob' or 'arraybuffer'.
|
|
170
|
+
*/
|
|
171
|
+
set binaryType(value) {
|
|
172
|
+
this._underlyingWebsocket.binaryType = value;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Sends data over the websocket.
|
|
176
|
+
*
|
|
177
|
+
* If the websocket is not connected and a buffer was provided on creation, the data will be added to the buffer.
|
|
178
|
+
* If no buffer was provided or the websocket was closed by the user, the data will be dropped.
|
|
179
|
+
*
|
|
180
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
|
|
181
|
+
* @param data to send.
|
|
182
|
+
*/
|
|
183
|
+
send(data) {
|
|
184
|
+
if (this.closedByUser)
|
|
185
|
+
return; // no-op if closed by user
|
|
186
|
+
if (this._underlyingWebsocket.readyState === this._underlyingWebsocket.OPEN) {
|
|
187
|
+
this._underlyingWebsocket.send(data); // websocket is connected, send data
|
|
188
|
+
}
|
|
189
|
+
else if (this.buffer !== undefined) {
|
|
190
|
+
this.buffer.add(data); // websocket is not connected, add data to buffer
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Close the websocket. No connection-retry will be attempted after this.
|
|
195
|
+
*
|
|
196
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
|
|
197
|
+
* @param code optional close code.
|
|
198
|
+
* @param reason optional close reason.
|
|
199
|
+
*/
|
|
200
|
+
close(code, reason) {
|
|
201
|
+
this.cancelScheduledConnectionRetry(); // cancel any scheduled retries
|
|
202
|
+
this._closedByUser = true; // mark websocket as closed by user
|
|
203
|
+
this._underlyingWebsocket.close(code, reason); // close underlying websocket with provided code and reason
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Adds an event listener for the given event-type.
|
|
207
|
+
*
|
|
208
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
|
|
209
|
+
* @param type of the event to add the listener for.
|
|
210
|
+
* @param listener to add.
|
|
211
|
+
* @param options to use when adding the listener.
|
|
212
|
+
*/
|
|
213
|
+
addEventListener(type, listener, options) {
|
|
214
|
+
this._options.listeners[type].push({ listener, options }); // add listener to list of listeners
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Removes one or more event listener for the given event-type that match the given listener and options.
|
|
218
|
+
*
|
|
219
|
+
* @param type of the event to remove the listener for.
|
|
220
|
+
* @param listener to remove.
|
|
221
|
+
* @param options that were used when the listener was added.
|
|
222
|
+
*/
|
|
223
|
+
removeEventListener(type, listener, options) {
|
|
224
|
+
const isListenerNotToBeRemoved = (l) => l.listener !== listener || l.options !== options;
|
|
225
|
+
this._options.listeners[type] =
|
|
226
|
+
this._options.listeners[type].filter(isListenerNotToBeRemoved); // only keep listeners that are not to be removed
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Creates a new browser-native websocket and connects it to the given URL with the given protocols
|
|
230
|
+
* and adds all event listeners to the browser-native websocket.
|
|
231
|
+
*
|
|
232
|
+
* @return the created browser-native websocket which is also stored in the '_underlyingWebsocket' property.
|
|
233
|
+
*/
|
|
234
|
+
tryConnect() {
|
|
235
|
+
this._underlyingWebsocket = new WebSocket(this.url, this.protocols); // create new browser-native websocket and add all event listeners
|
|
236
|
+
this._underlyingWebsocket.addEventListener(WebsocketEvent.open, this.handleOpenEvent);
|
|
237
|
+
this._underlyingWebsocket.addEventListener(WebsocketEvent.close, this.handleCloseEvent);
|
|
238
|
+
this._underlyingWebsocket.addEventListener(WebsocketEvent.error, this.handleErrorEvent);
|
|
239
|
+
this._underlyingWebsocket.addEventListener(WebsocketEvent.message, this.handleMessageEvent);
|
|
240
|
+
return this._underlyingWebsocket;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Removes all event listeners from the browser-native websocket and closes it.
|
|
244
|
+
*/
|
|
245
|
+
clearWebsocket() {
|
|
246
|
+
this._underlyingWebsocket.removeEventListener(WebsocketEvent.open, this.handleOpenEvent);
|
|
247
|
+
this._underlyingWebsocket.removeEventListener(WebsocketEvent.close, this.handleCloseEvent);
|
|
248
|
+
this._underlyingWebsocket.removeEventListener(WebsocketEvent.error, this.handleErrorEvent);
|
|
249
|
+
this._underlyingWebsocket.removeEventListener(WebsocketEvent.message, this.handleMessageEvent);
|
|
250
|
+
this._underlyingWebsocket.close();
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Dispatch an event to all listeners of the given event-type.
|
|
254
|
+
*
|
|
255
|
+
* @param type of the event to dispatch.
|
|
256
|
+
* @param event to dispatch.
|
|
257
|
+
*/
|
|
258
|
+
dispatchEvent(type, event) {
|
|
259
|
+
const eventListeners = this._options.listeners[type];
|
|
260
|
+
const newEventListeners = [];
|
|
261
|
+
eventListeners.forEach(({ listener, options }) => {
|
|
262
|
+
listener(this, event); // invoke listener with event
|
|
263
|
+
if (options === undefined ||
|
|
264
|
+
options.once === undefined ||
|
|
265
|
+
!options.once) {
|
|
266
|
+
newEventListeners.push({ listener, options }); // only keep listener if it isn't a once-listener
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
this._options.listeners[type] = newEventListeners; // replace old listeners with new listeners that don't include once-listeners
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Handles the given event by dispatching it to all listeners of the given event-type.
|
|
273
|
+
*
|
|
274
|
+
* @param type of the event to handle.
|
|
275
|
+
* @param event to handle.
|
|
276
|
+
*/
|
|
277
|
+
handleEvent(type, event) {
|
|
278
|
+
switch (type) {
|
|
279
|
+
case WebsocketEvent.close:
|
|
280
|
+
this.dispatchEvent(type, event);
|
|
281
|
+
this.scheduleConnectionRetryIfNeeded(); // schedule a new connection retry if the websocket was closed by the server
|
|
282
|
+
break;
|
|
283
|
+
case WebsocketEvent.open:
|
|
284
|
+
if (this.backoff !== undefined && this._lastConnection !== undefined) {
|
|
285
|
+
// websocket was reconnected, dispatch reconnect event and reset backoff
|
|
286
|
+
const detail = {
|
|
287
|
+
retries: this.backoff.retries,
|
|
288
|
+
lastConnection: new Date(this._lastConnection),
|
|
289
|
+
};
|
|
290
|
+
const event = new CustomEvent(WebsocketEvent.reconnect, {
|
|
291
|
+
detail,
|
|
292
|
+
});
|
|
293
|
+
this.dispatchEvent(WebsocketEvent.reconnect, event);
|
|
294
|
+
this.backoff.reset();
|
|
295
|
+
}
|
|
296
|
+
this._lastConnection = new Date();
|
|
297
|
+
this.dispatchEvent(type, event); // dispatch open event and send buffered data
|
|
298
|
+
this.sendBufferedData();
|
|
299
|
+
break;
|
|
300
|
+
case WebsocketEvent.retry:
|
|
301
|
+
this.dispatchEvent(type, event); // dispatch retry event and try to connect
|
|
302
|
+
this.clearWebsocket(); // clear the old websocket
|
|
303
|
+
this.tryConnect();
|
|
304
|
+
break;
|
|
305
|
+
default:
|
|
306
|
+
this.dispatchEvent(type, event); // dispatch event to all listeners of the given event-type
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Sends buffered data if there is a buffer defined.
|
|
312
|
+
*/
|
|
313
|
+
sendBufferedData() {
|
|
314
|
+
if (this.buffer === undefined) {
|
|
315
|
+
return; // no buffer defined, nothing to send
|
|
316
|
+
}
|
|
317
|
+
for (let ele = this.buffer.read(); ele !== undefined; ele = this.buffer.read()) {
|
|
318
|
+
this.send(ele); // send buffered data
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Schedules a connection-retry if there is a backoff defined and the websocket was not closed by the user.
|
|
323
|
+
*/
|
|
324
|
+
scheduleConnectionRetryIfNeeded() {
|
|
325
|
+
if (this.closedByUser) {
|
|
326
|
+
return; // user closed the websocket, no retry
|
|
327
|
+
}
|
|
328
|
+
if (this.backoff === undefined) {
|
|
329
|
+
return; // no backoff defined, no retry
|
|
330
|
+
}
|
|
331
|
+
const handleRetryEvent = (detail) => {
|
|
332
|
+
const event = new CustomEvent(WebsocketEvent.retry, { detail });
|
|
333
|
+
this.handleEvent(WebsocketEvent.retry, event);
|
|
334
|
+
};
|
|
335
|
+
// create retry event detail, depending on the 'instantReconnect' option
|
|
336
|
+
const retryEventDetail = {
|
|
337
|
+
backoff: this._options.retry.instantReconnect === true ? 0 : this.backoff.next(),
|
|
338
|
+
retries: this._options.retry.instantReconnect === true
|
|
339
|
+
? 0
|
|
340
|
+
: this.backoff.retries,
|
|
341
|
+
lastConnection: this._lastConnection,
|
|
342
|
+
};
|
|
343
|
+
// schedule a new connection-retry if the maximum number of retries is not reached yet
|
|
344
|
+
if (this._options.retry.maxRetries === undefined ||
|
|
345
|
+
retryEventDetail.retries <= this._options.retry.maxRetries) {
|
|
346
|
+
this.retryTimeout = window.setTimeout(() => handleRetryEvent(retryEventDetail), retryEventDetail.backoff);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Cancels the scheduled connection-retry, if there is one.
|
|
351
|
+
*/
|
|
352
|
+
cancelScheduledConnectionRetry() {
|
|
353
|
+
window.clearTimeout(this.retryTimeout);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
//# sourceMappingURL=websocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket.js","sourceRoot":"","sources":["../../../src/websocket.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,cAAc,GAMf,MAAM,mBAAmB,CAAC;AAG3B;;GAEG;AACH,MAAM,OAAO,SAAS;IAYpB;;;;;;OAMG;IACH,YACE,GAAW,EACX,SAA6B,EAC7B,OAA0B;;QAlBpB,kBAAa,GAAY,KAAK,CAAC,CAAC,+CAA+C;QAwSvF;;;WAGG;QACK,oBAAe,GAAG,CAAC,KAAY,EAAE,EAAE,CACzC,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAE/C;;;WAGG;QACK,qBAAgB,GAAG,CAAC,KAAY,EAAE,EAAE,CAC1C,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAEhD;;;WAGG;QACK,qBAAgB,GAAG,CAAC,KAAiB,EAAE,EAAE,CAC/C,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAEhD;;;WAGG;QACK,uBAAkB,GAAG,CAAC,KAAmB,EAAE,EAAE,CACnD,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;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face WebsocketBuffer<E = string | ArrayBufferLike | Blob | ArrayBufferView> {
|
|
5
|
+
/**
|
|
6
|
+
* Adds an element to the buffer.
|
|
7
|
+
* @param element the element to add
|
|
8
|
+
*/
|
|
9
|
+
add(element: E): void;
|
|
10
|
+
/**
|
|
11
|
+
* Reads an element from the buffer.
|
|
12
|
+
* @return an element from the buffer or undefined if the buffer is empty
|
|
13
|
+
*/
|
|
14
|
+
read(): E | undefined;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=websocket_buffer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_buffer.d.ts","sourceRoot":"","sources":["../../../src/websocket_buffer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe,CAC9B,CAAC,GAAG,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe;IAErD;;;OAGG;IACH,GAAG,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC;IAEtB;;;OAGG;IACH,IAAI,IAAI,CAAC,GAAG,SAAS,CAAC;CACvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_buffer.js","sourceRoot":"","sources":["../../../src/websocket_buffer.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Backoff } from "./backoff/backoff";
|
|
2
|
+
import { WebsocketEvent, WebsocketEventListener, WebsocketEventListenerOptions } from "./websocket_event";
|
|
3
|
+
import { Websocket } from "./websocket";
|
|
4
|
+
import { WebsocketBuffer } from "./websocket_buffer";
|
|
5
|
+
/**
|
|
6
|
+
* Builder for websockets.
|
|
7
|
+
*/
|
|
8
|
+
export declare class WebsocketBuilder {
|
|
9
|
+
private readonly _url;
|
|
10
|
+
private _protocols?;
|
|
11
|
+
private _options?;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new WebsocketBuilder.
|
|
14
|
+
*
|
|
15
|
+
* @param url the url to connect to
|
|
16
|
+
*/
|
|
17
|
+
constructor(url: string);
|
|
18
|
+
/**
|
|
19
|
+
* Getter for the url.
|
|
20
|
+
*
|
|
21
|
+
* @returns the url
|
|
22
|
+
*/
|
|
23
|
+
get url(): string;
|
|
24
|
+
/**
|
|
25
|
+
* Adds protocols to the websocket. Subsequent calls to this method will override the previously set protocols.
|
|
26
|
+
*
|
|
27
|
+
* @param protocols the protocols to add
|
|
28
|
+
*/
|
|
29
|
+
withProtocols(protocols: string | string[] | undefined): WebsocketBuilder;
|
|
30
|
+
/**
|
|
31
|
+
* Getter for the protocols.
|
|
32
|
+
*
|
|
33
|
+
* @returns the protocols, undefined if no protocols have been set
|
|
34
|
+
*/
|
|
35
|
+
get protocols(): string | string[] | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Sets the maximum number of retries before giving up. No limit if undefined.
|
|
38
|
+
*
|
|
39
|
+
* @param maxRetries the maximum number of retries before giving up
|
|
40
|
+
*/
|
|
41
|
+
withMaxRetries(maxRetries: number | undefined): WebsocketBuilder;
|
|
42
|
+
/**
|
|
43
|
+
* Getter for the maximum number of retries before giving up.
|
|
44
|
+
*
|
|
45
|
+
* @returns the maximum number of retries before giving up, undefined if no maximum has been set
|
|
46
|
+
*/
|
|
47
|
+
get maxRetries(): number | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Sets wether to reconnect immediately after a connection has been lost, ignoring the backoff strategy for the first retry.
|
|
50
|
+
*
|
|
51
|
+
* @param instantReconnect wether to reconnect immediately after a connection has been lost
|
|
52
|
+
*/
|
|
53
|
+
withInstantReconnect(instantReconnect: boolean | undefined): WebsocketBuilder;
|
|
54
|
+
/**
|
|
55
|
+
* Getter for wether to reconnect immediately after a connection has been lost, ignoring the backoff strategy for the first retry.
|
|
56
|
+
*
|
|
57
|
+
* @returns wether to reconnect immediately after a connection has been lost, undefined if no value has been set
|
|
58
|
+
*/
|
|
59
|
+
get instantReconnect(): boolean | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Adds a backoff to the websocket. Subsequent calls to this method will override the previously set backoff.
|
|
62
|
+
*
|
|
63
|
+
* @param backoff the backoff to add
|
|
64
|
+
*/
|
|
65
|
+
withBackoff(backoff: Backoff | undefined): WebsocketBuilder;
|
|
66
|
+
/**
|
|
67
|
+
* Getter for the backoff.
|
|
68
|
+
*
|
|
69
|
+
* @returns the backoff, undefined if no backoff has been set
|
|
70
|
+
*/
|
|
71
|
+
get backoff(): Backoff | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Adds a buffer to the websocket. Subsequent calls to this method will override the previously set buffer.
|
|
74
|
+
*
|
|
75
|
+
* @param buffer the buffer to add
|
|
76
|
+
*/
|
|
77
|
+
withBuffer(buffer: WebsocketBuffer | undefined): WebsocketBuilder;
|
|
78
|
+
/**
|
|
79
|
+
* Getter for the buffer.
|
|
80
|
+
*
|
|
81
|
+
* @returns the buffer, undefined if no buffer has been set
|
|
82
|
+
*/
|
|
83
|
+
get buffer(): WebsocketBuffer | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Adds an 'open' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
86
|
+
* called in the order they were added.
|
|
87
|
+
*
|
|
88
|
+
* @param listener the listener to add
|
|
89
|
+
* @param options the listener options
|
|
90
|
+
*/
|
|
91
|
+
onOpen(listener: WebsocketEventListener<WebsocketEvent.open>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
|
|
92
|
+
/**
|
|
93
|
+
* Adds an 'close' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
94
|
+
* called in the order they were added.
|
|
95
|
+
*
|
|
96
|
+
* @param listener the listener to add
|
|
97
|
+
* @param options the listener options
|
|
98
|
+
*/
|
|
99
|
+
onClose(listener: WebsocketEventListener<WebsocketEvent.close>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
|
|
100
|
+
/**
|
|
101
|
+
* Adds an 'error' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
102
|
+
* called in the order they were added.
|
|
103
|
+
*
|
|
104
|
+
* @param listener the listener to add
|
|
105
|
+
* @param options the listener options
|
|
106
|
+
*/
|
|
107
|
+
onError(listener: WebsocketEventListener<WebsocketEvent.error>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
|
|
108
|
+
/**
|
|
109
|
+
* Adds an 'message' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
110
|
+
* called in the order they were added.
|
|
111
|
+
*
|
|
112
|
+
* @param listener the listener to add
|
|
113
|
+
* @param options the listener options
|
|
114
|
+
*/
|
|
115
|
+
onMessage(listener: WebsocketEventListener<WebsocketEvent.message>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
|
|
116
|
+
/**
|
|
117
|
+
* Adds an 'retry' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
118
|
+
* called in the order they were added.
|
|
119
|
+
*
|
|
120
|
+
* @param listener the listener to add
|
|
121
|
+
* @param options the listener options
|
|
122
|
+
*/
|
|
123
|
+
onRetry(listener: WebsocketEventListener<WebsocketEvent.retry>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
|
|
124
|
+
/**
|
|
125
|
+
* Adds an 'reconnect' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
126
|
+
* called in the order they were added.
|
|
127
|
+
*
|
|
128
|
+
* @param listener the listener to add
|
|
129
|
+
* @param options the listener options
|
|
130
|
+
*/
|
|
131
|
+
onReconnect(listener: WebsocketEventListener<WebsocketEvent.reconnect>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
|
|
132
|
+
/**
|
|
133
|
+
* Builds the websocket.
|
|
134
|
+
*
|
|
135
|
+
* @return a new websocket, with the set options
|
|
136
|
+
*/
|
|
137
|
+
build(): Websocket;
|
|
138
|
+
/**
|
|
139
|
+
* Adds an event listener to the options.
|
|
140
|
+
*
|
|
141
|
+
* @param event the event to add the listener to
|
|
142
|
+
* @param listener the listener to add
|
|
143
|
+
* @param options the listener options
|
|
144
|
+
*/
|
|
145
|
+
private addListener;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=websocket_builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_builder.d.ts","sourceRoot":"","sources":["../../../src/websocket_builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EACL,cAAc,EACd,sBAAsB,EACtB,6BAA6B,EAC9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAE9B,OAAO,CAAC,UAAU,CAAC,CAAoB;IACvC,OAAO,CAAC,QAAQ,CAAC,CAAmB;IAEpC;;;;OAIG;gBACS,GAAG,EAAE,MAAM;IAIvB;;;;OAIG;IACH,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED;;;;OAIG;IACI,aAAa,CAClB,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,GACvC,gBAAgB;IAKnB;;;;OAIG;IACH,IAAI,SAAS,IAAI,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAE7C;IAED;;;;OAIG;IACI,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,gBAAgB;IAQvE;;;;OAIG;IACH,IAAI,UAAU,IAAI,MAAM,GAAG,SAAS,CAEnC;IAED;;;;OAIG;IACI,oBAAoB,CACzB,gBAAgB,EAAE,OAAO,GAAG,SAAS,GACpC,gBAAgB;IAQnB;;;;OAIG;IACH,IAAI,gBAAgB,IAAI,OAAO,GAAG,SAAS,CAE1C;IAED;;;;OAIG;IACI,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,gBAAgB;IAQlE;;;;OAIG;IACH,IAAI,OAAO,IAAI,OAAO,GAAG,SAAS,C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