reconnecter use getter for connection status.

This commit is contained in:
Miroslav Šedivý 2021-07-17 15:27:20 +02:00
parent 368261ca14
commit 69f617ef45
2 changed files with 15 additions and 8 deletions

View File

@ -29,6 +29,10 @@ class WebsocketReconnecter extends ReconnecterAbstract {
this._websocket.on('disconnected', (error) => this.emit('disconnect', error)) this._websocket.on('disconnected', (error) => this.emit('disconnect', error))
} }
public get connected() {
return this._websocket.connected
}
public async connect() { public async connect() {
let url = this._state.url let url = this._state.url
url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws' 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)) this._webrtc.on('disconnected', (error) => this.emit('disconnect', error))
} }
public get connected() {
return this._webrtc.connected
}
public async connect() { public async connect() {
this._websocket.send(EVENT.SIGNAL_REQUEST, { video: this._state.webrtc.video }) this._websocket.send(EVENT.SIGNAL_REQUEST, { video: this._state.webrtc.video })
} }

View File

@ -15,6 +15,10 @@ export abstract class ReconnecterAbstract extends EventEmitter<ReconnecterAbstra
} }
} }
public get connected(): boolean {
throw new Error("Getter'connected()' must be implemented.")
}
public async connect() { public async connect() {
throw new Error("Method 'connect()' must be implemented.") throw new Error("Method 'connect()' must be implemented.")
} }
@ -37,7 +41,6 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
private _timeout?: number private _timeout?: number
private _open = false private _open = false
private _connected = false
private _total_reconnects = 0 private _total_reconnects = 0
private _last_connected?: Date private _last_connected?: Date
@ -62,8 +65,6 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
this._timeout = undefined this._timeout = undefined
} }
this._connected = true
if (this._open) { if (this._open) {
this._last_connected = new Date() this._last_connected = new Date()
this.emit('connect') this.emit('connect')
@ -78,8 +79,6 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
this._timeout = undefined this._timeout = undefined
} }
this._connected = false
if (this._open) { if (this._open) {
this.emit('disconnect') this.emit('disconnect')
this.reconnect() this.reconnect()
@ -91,7 +90,7 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
} }
public get isConnected(): boolean { public get isConnected(): boolean {
return this._connected return this._conn.connected
} }
public get totalReconnects(): number { public get totalReconnects(): number {
@ -136,7 +135,7 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
this._last_connected = undefined this._last_connected = undefined
this.emit('close', error) this.emit('close', error)
if (this._connected) { if (this._conn.connected) {
this._conn.disconnect() this._conn.disconnect()
} }
} }
@ -147,7 +146,7 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
} }
public reconnect(): void { public reconnect(): void {
if (this._connected) { if (this._conn.connected) {
throw new Error('connection is already connected') throw new Error('connection is already connected')
} }