From 69f617ef45ef82959d1b90a525076faa8fa3c5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 17 Jul 2021 15:27:20 +0200 Subject: [PATCH] reconnecter use getter for connection status. --- src/component/internal/connection.ts | 8 ++++++++ src/component/utils/reconnecter.ts | 15 +++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/component/internal/connection.ts b/src/component/internal/connection.ts index e45ad100..7fbec204 100644 --- a/src/component/internal/connection.ts +++ b/src/component/internal/connection.ts @@ -29,6 +29,10 @@ class WebsocketReconnecter extends ReconnecterAbstract { this._websocket.on('disconnected', (error) => this.emit('disconnect', error)) } + public get connected() { + return this._websocket.connected + } + public async connect() { let url = this._state.url url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws' @@ -63,6 +67,10 @@ class WebrtcReconnecter extends ReconnecterAbstract { this._webrtc.on('disconnected', (error) => this.emit('disconnect', error)) } + public get connected() { + return this._webrtc.connected + } + public async connect() { this._websocket.send(EVENT.SIGNAL_REQUEST, { video: this._state.webrtc.video }) } diff --git a/src/component/utils/reconnecter.ts b/src/component/utils/reconnecter.ts index 4969247d..4e96e425 100644 --- a/src/component/utils/reconnecter.ts +++ b/src/component/utils/reconnecter.ts @@ -15,6 +15,10 @@ export abstract class ReconnecterAbstract extends EventEmitter { private _timeout?: number private _open = false - private _connected = false private _total_reconnects = 0 private _last_connected?: Date @@ -62,8 +65,6 @@ export class Reconnecter extends EventEmitter { this._timeout = undefined } - this._connected = true - if (this._open) { this._last_connected = new Date() this.emit('connect') @@ -78,8 +79,6 @@ export class Reconnecter extends EventEmitter { this._timeout = undefined } - this._connected = false - if (this._open) { this.emit('disconnect') this.reconnect() @@ -91,7 +90,7 @@ export class Reconnecter extends EventEmitter { } public get isConnected(): boolean { - return this._connected + return this._conn.connected } public get totalReconnects(): number { @@ -136,7 +135,7 @@ export class Reconnecter extends EventEmitter { this._last_connected = undefined this.emit('close', error) - if (this._connected) { + if (this._conn.connected) { this._conn.disconnect() } } @@ -147,7 +146,7 @@ export class Reconnecter extends EventEmitter { } public reconnect(): void { - if (this._connected) { + if (this._conn.connected) { throw new Error('connection is already connected') }