reconnector.

This commit is contained in:
Miroslav Šedivý 2021-07-26 23:23:09 +02:00
parent 558b6d2c0b
commit 5f1cca5ab2
4 changed files with 24 additions and 24 deletions

View File

@ -5,7 +5,7 @@ import * as EVENT from '../types/events'
import { NekoWebSocket } from './websocket'
import { NekoWebRTC } from './webrtc'
import { Connection, WebRTCStats } from '../types/state'
import { Reconnecter, ReconnecterAbstract } from '../utils/reconnecter'
import { Reconnector, ReconnectorAbstract } from '../utils/reconnector'
const WEBRTC_RECONN_MAX_LOSS = 25
const WEBRTC_RECONN_FAILED_ATTEMPTS = 5
@ -14,7 +14,7 @@ export interface NekoConnectionEvents {
disconnect: (error?: Error) => void
}
class WebsocketReconnecter extends ReconnecterAbstract {
class WebsocketReconnector extends ReconnectorAbstract {
private _state: Connection
private _websocket: NekoWebSocket
@ -60,7 +60,7 @@ class WebsocketReconnecter extends ReconnecterAbstract {
}
}
class WebrtcReconnecter extends ReconnecterAbstract {
class WebrtcReconnector extends ReconnectorAbstract {
private _state: Connection
private _websocket: NekoWebSocket
private _webrtc: NekoWebRTC
@ -106,18 +106,18 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
private _state: Connection
public websocket = new NekoWebSocket()
public _websocket_reconn: Reconnecter
public _websocket_reconn: Reconnector
public webrtc = new NekoWebRTC()
public _webrtc_reconn: Reconnecter
public _webrtc_reconn: Reconnector
constructor(state: Connection) {
super()
this._state = state
this._websocket_reconn = new Reconnecter(new WebsocketReconnecter(state, this.websocket), state.websocket.config)
this._webrtc_reconn = new Reconnecter(
new WebrtcReconnecter(state, this.websocket, this.webrtc),
this._websocket_reconn = new Reconnector(new WebsocketReconnector(state, this.websocket), state.websocket.config)
this._webrtc_reconn = new Reconnector(
new WebrtcReconnector(state, this.websocket, this.webrtc),
state.webrtc.config,
)

View File

@ -1,4 +1,4 @@
export interface ReconnecterConfig {
export interface ReconnectorConfig {
maxReconnects: number
timeoutMs: number
backoffMs: number

View File

@ -1,5 +1,5 @@
import * as webrtcTypes from './webrtc'
import * as reconnecterTypes from './reconnecter'
import * as reconnecterTypes from './reconnector'
export default interface State {
authenticated: boolean
@ -26,18 +26,18 @@ export interface Connection {
}
export interface WebSocket {
config: ReconnecterConfig
config: ReconnectorConfig
}
export interface WebRTC {
config: ReconnecterConfig
config: ReconnectorConfig
stats: WebRTCStats | null
video: string | null
videos: string[]
auto: boolean
}
export interface ReconnecterConfig extends reconnecterTypes.ReconnecterConfig {}
export interface ReconnectorConfig extends reconnecterTypes.ReconnectorConfig {}
export interface WebRTCStats extends webrtcTypes.WebRTCStats {}

View File

@ -1,16 +1,16 @@
import EventEmitter from 'eventemitter3'
import { ReconnecterConfig } from '../types/reconnecter'
import { ReconnectorConfig } from '../types/reconnector'
export interface ReconnecterAbstractEvents {
export interface ReconnectorAbstractEvents {
connect: () => void
disconnect: (error?: Error) => void
}
export abstract class ReconnecterAbstract extends EventEmitter<ReconnecterAbstractEvents> {
export abstract class ReconnectorAbstract extends EventEmitter<ReconnectorAbstractEvents> {
constructor() {
super()
if (this.constructor == ReconnecterAbstract) {
if (this.constructor == ReconnectorAbstract) {
throw new Error("Abstract classes can't be instantiated.")
}
}
@ -22,16 +22,16 @@ export abstract class ReconnecterAbstract extends EventEmitter<ReconnecterAbstra
public abstract destroy(): void
}
export interface ReconnecterEvents {
export interface ReconnectorEvents {
open: () => void
connect: () => void
disconnect: () => void
close: (error?: Error) => void
}
export class Reconnecter extends EventEmitter<ReconnecterEvents> {
private _conn: ReconnecterAbstract
private _config: ReconnecterConfig
export class Reconnector extends EventEmitter<ReconnectorEvents> {
private _conn: ReconnectorAbstract
private _config: ReconnectorConfig
private _timeout?: number
private _open = false
@ -41,7 +41,7 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
private _onConnectHandle: () => void
private _onDisconnectHandle: (error?: Error) => void
constructor(conn: ReconnecterAbstract, config?: ReconnecterConfig) {
constructor(conn: ReconnectorAbstract, config?: ReconnectorConfig) {
super()
this._conn = conn
@ -101,11 +101,11 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
return this._last_connected
}
public get config(): ReconnecterConfig {
public get config(): ReconnectorConfig {
return { ...this._config }
}
public set config(conf: ReconnecterConfig) {
public set config(conf: ReconnectorConfig) {
this._config = { ...conf }
if (this._config.maxReconnects > this._total_reconnects) {