in src/utils/ReconnectingPromisedWebSocket.ts [36:79]
async open(timeoutMs: number): Promise<Event> {
if (this.webSocket !== null) {
return Promise.reject(new Error('opened'));
}
this.webSocket = this.webSocketFactory.create(this.url, this.protocols, this.binaryType);
this.webSocket.addEventListener('close', (event: CloseEvent) => {
if (ReconnectingPromisedWebSocket.normalClosureCodes.indexOf(event.code) <= -1) {
try {
const timeout = this.backoff.nextBackoffAmountMs();
this.timeoutScheduler = new TimeoutScheduler(timeout);
this.timeoutScheduler.start(() => {
this.timeoutScheduler.stop();
this.open(timeoutMs)
.catch((error: ErrorEvent) => {
this.dispatchEvent(new CustomEvent('reconnect_error', { detail: error }));
})
.then(() => {});
});
this.dispatchEvent(new CustomEvent('reconnect'));
} catch (e) {
this.dispatchEvent(event);
}
} else {
this.dispatchEvent(event);
}
this.webSocket = null;
});
this.webSocket.addEventListener('message', (event: MessageEvent) => {
this.dispatchEvent(event);
});
this.webSocket.addEventListener('open', (event: Event) => {
this.didOpenWebSocket();
this.dispatchEvent(event);
});
return this.webSocket.open(timeoutMs).catch(error => {
this.webSocket = null;
throw error;
});
}