From 97a213c9b30e9092151b8648b93d15fa89f2b2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 20 Jun 2021 23:29:00 +0200 Subject: [PATCH] ws connect not async. --- src/component/internal/websocket.ts | 31 ++++++++--------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/component/internal/websocket.ts b/src/component/internal/websocket.ts index d2560b55..cf91275d 100644 --- a/src/component/internal/websocket.ts +++ b/src/component/internal/websocket.ts @@ -1,9 +1,6 @@ import EventEmitter from 'eventemitter3' import { Logger } from '../utils/logger' -export const connTimeout = 15000 -export const reconnInterval = 1000 - export interface NekoWebSocketEvents { connecting: () => void connected: () => void @@ -29,7 +26,7 @@ export class NekoWebSocket extends EventEmitter { return typeof this._ws !== 'undefined' && this._ws.readyState === WebSocket.OPEN } - public async connect(url: string) { + public connect(url: string) { if (!this.supported) { throw new Error('browser does not support websockets') } @@ -43,27 +40,15 @@ export class NekoWebSocket extends EventEmitter { this.disconnect() } - await new Promise((res, rej) => { - this._ws = new WebSocket(url) + this._ws = new WebSocket(url) - this._log.info(`connecting`) - this.emit('connecting') + this._log.info(`connecting`) + this.emit('connecting') - this._ws.onclose = rej.bind(this, new Error('connection close')) - this._ws.onerror = rej.bind(this, new Error('connection error')) - this._ws.onmessage = this.onMessage.bind(this) - - const timeout = window.setTimeout(rej.bind(this, new Error('connection timeout')), connTimeout) - this._ws.onopen = () => { - window.clearTimeout(timeout) - - this._ws!.onclose = this.onDisconnected.bind(this, 'close') - this._ws!.onerror = this.onDisconnected.bind(this, 'error') - - this.onConnected() - res() - } - }) + this._ws.onopen = this.onConnected.bind(this) + this._ws.onclose = this.onDisconnected.bind(this, 'close') + this._ws.onerror = this.onDisconnected.bind(this, 'error') + this._ws.onmessage = this.onMessage.bind(this) } public disconnect() {