websocket-ts 2.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.
Files changed (113) hide show
  1. package/dist/cjs/src/backoff/backoff.d.ts +24 -0
  2. package/dist/cjs/src/backoff/backoff.d.ts.map +1 -0
  3. package/dist/cjs/src/backoff/backoff.js +3 -0
  4. package/dist/cjs/src/backoff/backoff.js.map +1 -0
  5. package/dist/cjs/src/backoff/constantbackoff.d.ts +18 -0
  6. package/dist/cjs/src/backoff/constantbackoff.d.ts.map +1 -0
  7. package/dist/cjs/src/backoff/constantbackoff.js +43 -0
  8. package/dist/cjs/src/backoff/constantbackoff.js.map +1 -0
  9. package/dist/cjs/src/backoff/exponentialbackoff.d.ts +48 -0
  10. package/dist/cjs/src/backoff/exponentialbackoff.d.ts.map +1 -0
  11. package/dist/cjs/src/backoff/exponentialbackoff.js +81 -0
  12. package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -0
  13. package/dist/cjs/src/backoff/linearbackoff.d.ts +51 -0
  14. package/dist/cjs/src/backoff/linearbackoff.d.ts.map +1 -0
  15. package/dist/cjs/src/backoff/linearbackoff.js +89 -0
  16. package/dist/cjs/src/backoff/linearbackoff.js.map +1 -0
  17. package/dist/cjs/src/index.d.ts +14 -0
  18. package/dist/cjs/src/index.d.ts.map +1 -0
  19. package/dist/cjs/src/index.js +20 -0
  20. package/dist/cjs/src/index.js.map +1 -0
  21. package/dist/cjs/src/queue/array_queue.d.ts +17 -0
  22. package/dist/cjs/src/queue/array_queue.d.ts.map +1 -0
  23. package/dist/cjs/src/queue/array_queue.js +36 -0
  24. package/dist/cjs/src/queue/array_queue.js.map +1 -0
  25. package/dist/cjs/src/queue/queue.d.ts +41 -0
  26. package/dist/cjs/src/queue/queue.d.ts.map +1 -0
  27. package/dist/cjs/src/queue/queue.js +3 -0
  28. package/dist/cjs/src/queue/queue.js.map +1 -0
  29. package/dist/cjs/src/queue/ring_queue.d.ts +20 -0
  30. package/dist/cjs/src/queue/ring_queue.d.ts.map +1 -0
  31. package/dist/cjs/src/queue/ring_queue.js +57 -0
  32. package/dist/cjs/src/queue/ring_queue.js.map +1 -0
  33. package/dist/cjs/src/websocket.d.ts +206 -0
  34. package/dist/cjs/src/websocket.d.ts.map +1 -0
  35. package/dist/cjs/src/websocket.js +434 -0
  36. package/dist/cjs/src/websocket.js.map +1 -0
  37. package/dist/cjs/src/websocket_buffer.d.ts +16 -0
  38. package/dist/cjs/src/websocket_buffer.d.ts.map +1 -0
  39. package/dist/cjs/src/websocket_buffer.js +3 -0
  40. package/dist/cjs/src/websocket_buffer.js.map +1 -0
  41. package/dist/cjs/src/websocket_builder.d.ts +147 -0
  42. package/dist/cjs/src/websocket_builder.d.ts.map +1 -0
  43. package/dist/cjs/src/websocket_builder.js +263 -0
  44. package/dist/cjs/src/websocket_builder.js.map +1 -0
  45. package/dist/cjs/src/websocket_event.d.ts +67 -0
  46. package/dist/cjs/src/websocket_event.d.ts.map +1 -0
  47. package/dist/cjs/src/websocket_event.js +22 -0
  48. package/dist/cjs/src/websocket_event.js.map +1 -0
  49. package/dist/cjs/src/websocket_options.d.ts +21 -0
  50. package/dist/cjs/src/websocket_options.d.ts.map +1 -0
  51. package/dist/cjs/src/websocket_options.js +3 -0
  52. package/dist/cjs/src/websocket_options.js.map +1 -0
  53. package/dist/cjs/src/websocket_retry_options.d.ts +20 -0
  54. package/dist/cjs/src/websocket_retry_options.d.ts.map +1 -0
  55. package/dist/cjs/src/websocket_retry_options.js +3 -0
  56. package/dist/cjs/src/websocket_retry_options.js.map +1 -0
  57. package/dist/esm/src/backoff/backoff.d.ts +24 -0
  58. package/dist/esm/src/backoff/backoff.d.ts.map +1 -0
  59. package/dist/esm/src/backoff/backoff.js +2 -0
  60. package/dist/esm/src/backoff/backoff.js.map +1 -0
  61. package/dist/esm/src/backoff/constantbackoff.d.ts +18 -0
  62. package/dist/esm/src/backoff/constantbackoff.d.ts.map +1 -0
  63. package/dist/esm/src/backoff/constantbackoff.js +30 -0
  64. package/dist/esm/src/backoff/constantbackoff.js.map +1 -0
  65. package/dist/esm/src/backoff/exponentialbackoff.d.ts +48 -0
  66. package/dist/esm/src/backoff/exponentialbackoff.d.ts.map +1 -0
  67. package/dist/esm/src/backoff/exponentialbackoff.js +68 -0
  68. package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -0
  69. package/dist/esm/src/backoff/linearbackoff.d.ts +51 -0
  70. package/dist/esm/src/backoff/linearbackoff.d.ts.map +1 -0
  71. package/dist/esm/src/backoff/linearbackoff.js +76 -0
  72. package/dist/esm/src/backoff/linearbackoff.js.map +1 -0
  73. package/dist/esm/src/index.d.ts +14 -0
  74. package/dist/esm/src/index.d.ts.map +1 -0
  75. package/dist/esm/src/index.js +9 -0
  76. package/dist/esm/src/index.js.map +1 -0
  77. package/dist/esm/src/queue/array_queue.d.ts +17 -0
  78. package/dist/esm/src/queue/array_queue.d.ts.map +1 -0
  79. package/dist/esm/src/queue/array_queue.js +31 -0
  80. package/dist/esm/src/queue/array_queue.js.map +1 -0
  81. package/dist/esm/src/queue/queue.d.ts +41 -0
  82. package/dist/esm/src/queue/queue.d.ts.map +1 -0
  83. package/dist/esm/src/queue/queue.js +2 -0
  84. package/dist/esm/src/queue/queue.js.map +1 -0
  85. package/dist/esm/src/queue/ring_queue.d.ts +20 -0
  86. package/dist/esm/src/queue/ring_queue.d.ts.map +1 -0
  87. package/dist/esm/src/queue/ring_queue.js +52 -0
  88. package/dist/esm/src/queue/ring_queue.js.map +1 -0
  89. package/dist/esm/src/websocket.d.ts +206 -0
  90. package/dist/esm/src/websocket.d.ts.map +1 -0
  91. package/dist/esm/src/websocket.js +356 -0
  92. package/dist/esm/src/websocket.js.map +1 -0
  93. package/dist/esm/src/websocket_buffer.d.ts +16 -0
  94. package/dist/esm/src/websocket_buffer.d.ts.map +1 -0
  95. package/dist/esm/src/websocket_buffer.js +2 -0
  96. package/dist/esm/src/websocket_buffer.js.map +1 -0
  97. package/dist/esm/src/websocket_builder.d.ts +147 -0
  98. package/dist/esm/src/websocket_builder.d.ts.map +1 -0
  99. package/dist/esm/src/websocket_builder.js +213 -0
  100. package/dist/esm/src/websocket_builder.js.map +1 -0
  101. package/dist/esm/src/websocket_event.d.ts +67 -0
  102. package/dist/esm/src/websocket_event.d.ts.map +1 -0
  103. package/dist/esm/src/websocket_event.js +19 -0
  104. package/dist/esm/src/websocket_event.js.map +1 -0
  105. package/dist/esm/src/websocket_options.d.ts +21 -0
  106. package/dist/esm/src/websocket_options.d.ts.map +1 -0
  107. package/dist/esm/src/websocket_options.js +2 -0
  108. package/dist/esm/src/websocket_options.js.map +1 -0
  109. package/dist/esm/src/websocket_retry_options.d.ts +20 -0
  110. package/dist/esm/src/websocket_retry_options.d.ts.map +1 -0
  111. package/dist/esm/src/websocket_retry_options.js +2 -0
  112. package/dist/esm/src/websocket_retry_options.js.map +1 -0
  113. package/package.json +1 -1
@@ -0,0 +1,206 @@
1
+ import { Backoff } from "./backoff/backoff";
2
+ import { WebsocketBuffer } from "./websocket_buffer";
3
+ import { WebsocketEvent, WebsocketEventListener, WebsocketEventListenerOptions } from "./websocket_event";
4
+ import { WebsocketOptions } from "./websocket_options";
5
+ /**
6
+ * A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.
7
+ */
8
+ export declare class Websocket {
9
+ private readonly _url;
10
+ private readonly _protocols?;
11
+ private _closedByUser;
12
+ private _lastConnection?;
13
+ private _underlyingWebsocket;
14
+ private retryTimeout?;
15
+ private _options;
16
+ /**
17
+ * Creates a new websocket.
18
+ *
19
+ * @param url to connect to.
20
+ * @param protocols optional protocols to use.
21
+ * @param options optional options to use.
22
+ */
23
+ constructor(url: string, protocols?: string | string[], options?: WebsocketOptions);
24
+ /**
25
+ * Getter for the url.
26
+ *
27
+ * @return the url.
28
+ */
29
+ get url(): string;
30
+ /**
31
+ * Getter for the protocols.
32
+ *
33
+ * @return the protocols, or undefined if none were provided.
34
+ */
35
+ get protocols(): string | string[] | undefined;
36
+ /**
37
+ * Getter for the buffer.
38
+ *
39
+ * @return the buffer, or undefined if none was provided.
40
+ */
41
+ get buffer(): WebsocketBuffer | undefined;
42
+ /**
43
+ * Getter for the maxRetries.
44
+ *
45
+ * @return the maxRetries, or undefined if none was provided (no limit).
46
+ */
47
+ get maxRetries(): number | undefined;
48
+ /**
49
+ * Getter for the instantReconnect.
50
+ *
51
+ * @return the instantReconnect, or undefined if none was provided.
52
+ */
53
+ get instantReconnect(): boolean | undefined;
54
+ /**
55
+ * Getter for the backoff.
56
+ *
57
+ * @return the backoff, or undefined if none was provided.
58
+ */
59
+ get backoff(): Backoff | undefined;
60
+ /**
61
+ * Whether the websocket was closed by the user. A websocket is closed by the user if the close().
62
+ *
63
+ * @return true if the websocket was closed by the user, false otherwise.
64
+ */
65
+ get closedByUser(): boolean;
66
+ /**
67
+ * Getter for the last 'open' event, e.g. the last time the websocket was connected.
68
+ *
69
+ * @return the last 'open' event, or undefined if the websocket was never connected.
70
+ */
71
+ get lastConnection(): Date | undefined;
72
+ /**
73
+ * Getter for the underlying websocket. This can be used to access the browser's native websocket directly.
74
+ *
75
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
76
+ * @return the underlying websocket.
77
+ */
78
+ get underlyingWebsocket(): WebSocket;
79
+ /**
80
+ * Getter for the readyState of the underlying websocket.
81
+ *
82
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
83
+ * @return the readyState of the underlying websocket.
84
+ */
85
+ get readyState(): number;
86
+ /**
87
+ * Getter for the bufferedAmount of the underlying websocket.
88
+ *
89
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount
90
+ * @return the bufferedAmount of the underlying websocket.
91
+ */
92
+ get bufferedAmount(): number;
93
+ /**
94
+ * Getter for the extensions of the underlying websocket.
95
+ *
96
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/extensions
97
+ * @return the extensions of the underlying websocket.
98
+ */
99
+ get extensions(): string;
100
+ /**
101
+ * Getter for the binaryType of the underlying websocket.
102
+ *
103
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType
104
+ * @return the binaryType of the underlying websocket.
105
+ */
106
+ get binaryType(): BinaryType;
107
+ /**
108
+ * Setter for the binaryType of the underlying websocket.
109
+ *
110
+ * @param value to set, 'blob' or 'arraybuffer'.
111
+ */
112
+ set binaryType(value: BinaryType);
113
+ /**
114
+ * Sends data over the websocket.
115
+ *
116
+ * If the websocket is not connected and a buffer was provided on creation, the data will be added to the buffer.
117
+ * If no buffer was provided or the websocket was closed by the user, the data will be dropped.
118
+ *
119
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
120
+ * @param data to send.
121
+ */
122
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
123
+ /**
124
+ * Close the websocket. No connection-retry will be attempted after this.
125
+ *
126
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
127
+ * @param code optional close code.
128
+ * @param reason optional close reason.
129
+ */
130
+ close(code?: number, reason?: string): void;
131
+ /**
132
+ * Adds an event listener for the given event-type.
133
+ *
134
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
135
+ * @param type of the event to add the listener for.
136
+ * @param listener to add.
137
+ * @param options to use when adding the listener.
138
+ */
139
+ addEventListener<K extends WebsocketEvent>(type: K, listener: WebsocketEventListener<K>, options?: WebsocketEventListenerOptions): void;
140
+ /**
141
+ * Removes one or more event listener for the given event-type that match the given listener and options.
142
+ *
143
+ * @param type of the event to remove the listener for.
144
+ * @param listener to remove.
145
+ * @param options that were used when the listener was added.
146
+ */
147
+ removeEventListener<K extends WebsocketEvent>(type: K, listener: WebsocketEventListener<K>, options?: WebsocketEventListenerOptions): void;
148
+ /**
149
+ * Creates a new browser-native websocket and connects it to the given URL with the given protocols
150
+ * and adds all event listeners to the browser-native websocket.
151
+ *
152
+ * @return the created browser-native websocket which is also stored in the '_underlyingWebsocket' property.
153
+ */
154
+ private tryConnect;
155
+ /**
156
+ * Removes all event listeners from the browser-native websocket and closes it.
157
+ */
158
+ private clearWebsocket;
159
+ /**
160
+ * Handles the 'open' event of the browser-native websocket.
161
+ * @param event to handle.
162
+ */
163
+ private handleOpenEvent;
164
+ /**
165
+ * Handles the 'error' event of the browser-native websocket.
166
+ * @param event to handle.
167
+ */
168
+ private handleErrorEvent;
169
+ /**
170
+ * Handles the 'close' event of the browser-native websocket.
171
+ * @param event to handle.
172
+ */
173
+ private handleCloseEvent;
174
+ /**
175
+ * Handles the 'message' event of the browser-native websocket.
176
+ * @param event to handle.
177
+ */
178
+ private handleMessageEvent;
179
+ /**
180
+ * Dispatch an event to all listeners of the given event-type.
181
+ *
182
+ * @param type of the event to dispatch.
183
+ * @param event to dispatch.
184
+ */
185
+ private dispatchEvent;
186
+ /**
187
+ * Handles the given event by dispatching it to all listeners of the given event-type.
188
+ *
189
+ * @param type of the event to handle.
190
+ * @param event to handle.
191
+ */
192
+ private handleEvent;
193
+ /**
194
+ * Sends buffered data if there is a buffer defined.
195
+ */
196
+ private sendBufferedData;
197
+ /**
198
+ * Schedules a connection-retry if there is a backoff defined and the websocket was not closed by the user.
199
+ */
200
+ private scheduleConnectionRetryIfNeeded;
201
+ /**
202
+ * Cancels the scheduled connection-retry, if there is one.
203
+ */
204
+ private cancelScheduledConnectionRetry;
205
+ }
206
+ //# sourceMappingURL=websocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAGL,cAAc,EACd,sBAAsB,EACtB,6BAA6B,EAI9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;GAEG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IAED;;;;;;;;OAQG;IACI,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe,GAAG,IAAI;IAY1E;;;;;;OAMG;IACI,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAMlD;;;;;;;OAOG;IACI,gBAAgB,CAAC,CAAC,SAAS,cAAc,EAC9C,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,sBAAsB,CAAC,CAAC,CAAC,EACnC,OAAO,CAAC,EAAE,6BAA6B,GACtC,IAAI;IAIP;;;;;;OAMG;IACI,mBAAmB,CAAC,CAAC,SAAS,cAAc,EACjD,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,sBAAsB,CAAC,CAAC,CAAC,EACnC,OAAO,CAAC,EAAE,6BAA6B,GACtC,IAAI;IASP;;;;;OAKG;IACH,OAAO,CAAC,UAAU;IAsBlB;;OAEG;IACH,OAAO,CAAC,cAAc;IAoBtB;;;OAGG;IACH,OAAO,CAAC,eAAe,CACwB;IAE/C;;;OAGG;IACH,OAAO,CAAC,gBAAgB,CACwB;IAEhD;;;OAGG;IACH,OAAO,CAAC,gBAAgB,CACwB;IAEhD;;;OAGG;IACH,OAAO,CAAC,kBAAkB,CACwB;IAElD;;;;;OAKG;IACH,OAAO,CAAC,aAAa;IAuBrB;;;;;OAKG;IACH,OAAO,CAAC,WAAW;IAyCnB;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAcxB;;OAEG;IACH,OAAO,CAAC,+BAA+B;IAuCvC;;OAEG;IACH,OAAO,CAAC,8BAA8B;CAGvC"}
@@ -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;QA9ShD,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAE5B,oEAAoE;QACpE,IAA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pD,MAAM,KAAK,GAAkC,IAAI,WAAW,CAC1D,cAAc,CAAC,KAAK,EACpB,EAAE,MAAM,EAAE,CACX,CAAC;YACF,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAChD,CAAC,CAAC;QAEF,wEAAwE;QACxE,MAAM,gBAAgB,GAAqB;YACzC,OAAO,EACL,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;YACzE,OAAO,EACL,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,KAAK,IAAI;gBAC3C,CAAC,CAAC,CAAC;gBACH,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO;YAC1B,cAAc,EAAE,IAAI,CAAC,eAAe;SACrC,CAAC;QAEF,sFAAsF;QACtF,IACE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,KAAK,SAAS;YAC5C,gBAAgB,CAAC,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,EAC1D;YACA,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,UAAU,CACnC,GAAG,EAAE,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,EACxC,gBAAgB,CAAC,OAAO,CACzB,CAAC;SACH;IACH,CAAC;IAED;;OAEG;IACK,8BAA8B;QACpC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACzC,CAAC;CACF"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * A WebsocketBuffer is used to store messages temporarily until they can be sent.
3
+ */
4
+ export interface 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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=websocket_buffer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket_buffer.js","sourceRoot":"","sources":["../../../src/websocket_buffer.ts"],"names":[],"mappings":""}