Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d264bad

Browse files
authoredFeb 7, 2020
Merge pull request #123 from Nodeigi/fix_compatibility_with_websocket
Fix compatibility with WebSocket
2 parents 179751a + d7c312f commit d264bad

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed
 

‎reconnecting-websocket.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export default class ReconnectingWebSocket {
7272
private _connectTimeout: any;
7373
private _shouldReconnect = true;
7474
private _connectLock = false;
75-
private _binaryType = 'blob';
75+
private _binaryType: BinaryType = 'blob';
7676
private _closeCalled = false;
7777
private _messageQueue: Message[] = [];
7878

@@ -116,14 +116,13 @@ export default class ReconnectingWebSocket {
116116
return ReconnectingWebSocket.CLOSED;
117117
}
118118

119-
get binaryType(): string {
119+
get binaryType() {
120120
return this._ws ? this._ws.binaryType : this._binaryType;
121121
}
122122

123-
set binaryType(value: string) {
123+
set binaryType(value: BinaryType) {
124124
this._binaryType = value;
125125
if (this._ws) {
126-
// @ts-ignore
127126
this._ws.binaryType = value;
128127
}
129128
}
@@ -194,23 +193,23 @@ export default class ReconnectingWebSocket {
194193
/**
195194
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
196195
*/
197-
public onclose?: (event: Events.CloseEvent) => void = undefined;
196+
public onclose: ((event: Events.CloseEvent) => void) | null = null;
198197

199198
/**
200199
* An event listener to be called when an error occurs
201200
*/
202-
public onerror?: (event: Events.ErrorEvent) => void = undefined;
201+
public onerror: ((event: Events.ErrorEvent) => void) | null = null;
203202

204203
/**
205204
* An event listener to be called when a message is received from the server
206205
*/
207-
public onmessage?: (event: MessageEvent) => void = undefined;
206+
public onmessage: ((event: MessageEvent) => void) | null = null;
208207

209208
/**
210209
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
211210
* this indicates that the connection is ready to send and receive data
212211
*/
213-
public onopen?: (event: Events.Event) => void = undefined;
212+
public onopen: ((event: Event) => void) | null = null;
214213

215214
/**
216215
* Closes the WebSocket connection or connection attempt, if any. If the connection is already
@@ -276,6 +275,16 @@ export default class ReconnectingWebSocket {
276275
}
277276
}
278277

278+
public dispatchEvent(event: Event) {
279+
const listeners = this._listeners[event.type as keyof Events.WebSocketEventListenerMap];
280+
if (listeners) {
281+
for (const listener of listeners) {
282+
this._callEventListener(event, listener);
283+
}
284+
}
285+
return true;
286+
}
287+
279288
/**
280289
* Removes an event listener
281290
*/
@@ -372,7 +381,6 @@ export default class ReconnectingWebSocket {
372381
this._ws = this._protocols
373382
? new WebSocket(url, this._protocols)
374383
: new WebSocket(url);
375-
// @ts-ignore
376384
this._ws!.binaryType = this._binaryType;
377385
this._connectLock = false;
378386
this._addListeners();
@@ -418,14 +426,13 @@ export default class ReconnectingWebSocket {
418426
}
419427
}
420428

421-
private _handleOpen = (event: Events.Event) => {
429+
private _handleOpen = (event: Event) => {
422430
this._debug('open event');
423431
const {minUptime = DEFAULT.minUptime} = this._options;
424432

425433
clearTimeout(this._connectTimeout);
426434
this._uptimeTimeout = setTimeout(() => this._acceptOpen(), minUptime);
427435

428-
// @ts-ignore
429436
this._ws!.binaryType = this._binaryType;
430437

431438
// send enqueued messages (messages sent before websocket open event)

0 commit comments

Comments
 (0)
Please sign in to comment.