mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
reconnector.
This commit is contained in:
parent
558b6d2c0b
commit
5f1cca5ab2
@ -5,7 +5,7 @@ import * as EVENT from '../types/events'
|
|||||||
import { NekoWebSocket } from './websocket'
|
import { NekoWebSocket } from './websocket'
|
||||||
import { NekoWebRTC } from './webrtc'
|
import { NekoWebRTC } from './webrtc'
|
||||||
import { Connection, WebRTCStats } from '../types/state'
|
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_MAX_LOSS = 25
|
||||||
const WEBRTC_RECONN_FAILED_ATTEMPTS = 5
|
const WEBRTC_RECONN_FAILED_ATTEMPTS = 5
|
||||||
@ -14,7 +14,7 @@ export interface NekoConnectionEvents {
|
|||||||
disconnect: (error?: Error) => void
|
disconnect: (error?: Error) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
class WebsocketReconnecter extends ReconnecterAbstract {
|
class WebsocketReconnector extends ReconnectorAbstract {
|
||||||
private _state: Connection
|
private _state: Connection
|
||||||
private _websocket: NekoWebSocket
|
private _websocket: NekoWebSocket
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ class WebsocketReconnecter extends ReconnecterAbstract {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class WebrtcReconnecter extends ReconnecterAbstract {
|
class WebrtcReconnector extends ReconnectorAbstract {
|
||||||
private _state: Connection
|
private _state: Connection
|
||||||
private _websocket: NekoWebSocket
|
private _websocket: NekoWebSocket
|
||||||
private _webrtc: NekoWebRTC
|
private _webrtc: NekoWebRTC
|
||||||
@ -106,18 +106,18 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
|||||||
private _state: Connection
|
private _state: Connection
|
||||||
|
|
||||||
public websocket = new NekoWebSocket()
|
public websocket = new NekoWebSocket()
|
||||||
public _websocket_reconn: Reconnecter
|
public _websocket_reconn: Reconnector
|
||||||
|
|
||||||
public webrtc = new NekoWebRTC()
|
public webrtc = new NekoWebRTC()
|
||||||
public _webrtc_reconn: Reconnecter
|
public _webrtc_reconn: Reconnector
|
||||||
|
|
||||||
constructor(state: Connection) {
|
constructor(state: Connection) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this._state = state
|
this._state = state
|
||||||
this._websocket_reconn = new Reconnecter(new WebsocketReconnecter(state, this.websocket), state.websocket.config)
|
this._websocket_reconn = new Reconnector(new WebsocketReconnector(state, this.websocket), state.websocket.config)
|
||||||
this._webrtc_reconn = new Reconnecter(
|
this._webrtc_reconn = new Reconnector(
|
||||||
new WebrtcReconnecter(state, this.websocket, this.webrtc),
|
new WebrtcReconnector(state, this.websocket, this.webrtc),
|
||||||
state.webrtc.config,
|
state.webrtc.config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export interface ReconnecterConfig {
|
export interface ReconnectorConfig {
|
||||||
maxReconnects: number
|
maxReconnects: number
|
||||||
timeoutMs: number
|
timeoutMs: number
|
||||||
backoffMs: number
|
backoffMs: number
|
@ -1,5 +1,5 @@
|
|||||||
import * as webrtcTypes from './webrtc'
|
import * as webrtcTypes from './webrtc'
|
||||||
import * as reconnecterTypes from './reconnecter'
|
import * as reconnecterTypes from './reconnector'
|
||||||
|
|
||||||
export default interface State {
|
export default interface State {
|
||||||
authenticated: boolean
|
authenticated: boolean
|
||||||
@ -26,18 +26,18 @@ export interface Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface WebSocket {
|
export interface WebSocket {
|
||||||
config: ReconnecterConfig
|
config: ReconnectorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WebRTC {
|
export interface WebRTC {
|
||||||
config: ReconnecterConfig
|
config: ReconnectorConfig
|
||||||
stats: WebRTCStats | null
|
stats: WebRTCStats | null
|
||||||
video: string | null
|
video: string | null
|
||||||
videos: string[]
|
videos: string[]
|
||||||
auto: boolean
|
auto: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ReconnecterConfig extends reconnecterTypes.ReconnecterConfig {}
|
export interface ReconnectorConfig extends reconnecterTypes.ReconnectorConfig {}
|
||||||
|
|
||||||
export interface WebRTCStats extends webrtcTypes.WebRTCStats {}
|
export interface WebRTCStats extends webrtcTypes.WebRTCStats {}
|
||||||
|
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
import EventEmitter from 'eventemitter3'
|
import EventEmitter from 'eventemitter3'
|
||||||
import { ReconnecterConfig } from '../types/reconnecter'
|
import { ReconnectorConfig } from '../types/reconnector'
|
||||||
|
|
||||||
export interface ReconnecterAbstractEvents {
|
export interface ReconnectorAbstractEvents {
|
||||||
connect: () => void
|
connect: () => void
|
||||||
disconnect: (error?: Error) => void
|
disconnect: (error?: Error) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class ReconnecterAbstract extends EventEmitter<ReconnecterAbstractEvents> {
|
export abstract class ReconnectorAbstract extends EventEmitter<ReconnectorAbstractEvents> {
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
if (this.constructor == ReconnecterAbstract) {
|
if (this.constructor == ReconnectorAbstract) {
|
||||||
throw new Error("Abstract classes can't be instantiated.")
|
throw new Error("Abstract classes can't be instantiated.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -22,16 +22,16 @@ export abstract class ReconnecterAbstract extends EventEmitter<ReconnecterAbstra
|
|||||||
public abstract destroy(): void
|
public abstract destroy(): void
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ReconnecterEvents {
|
export interface ReconnectorEvents {
|
||||||
open: () => void
|
open: () => void
|
||||||
connect: () => void
|
connect: () => void
|
||||||
disconnect: () => void
|
disconnect: () => void
|
||||||
close: (error?: Error) => void
|
close: (error?: Error) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Reconnecter extends EventEmitter<ReconnecterEvents> {
|
export class Reconnector extends EventEmitter<ReconnectorEvents> {
|
||||||
private _conn: ReconnecterAbstract
|
private _conn: ReconnectorAbstract
|
||||||
private _config: ReconnecterConfig
|
private _config: ReconnectorConfig
|
||||||
private _timeout?: number
|
private _timeout?: number
|
||||||
|
|
||||||
private _open = false
|
private _open = false
|
||||||
@ -41,7 +41,7 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
|
|||||||
private _onConnectHandle: () => void
|
private _onConnectHandle: () => void
|
||||||
private _onDisconnectHandle: (error?: Error) => void
|
private _onDisconnectHandle: (error?: Error) => void
|
||||||
|
|
||||||
constructor(conn: ReconnecterAbstract, config?: ReconnecterConfig) {
|
constructor(conn: ReconnectorAbstract, config?: ReconnectorConfig) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this._conn = conn
|
this._conn = conn
|
||||||
@ -101,11 +101,11 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
|
|||||||
return this._last_connected
|
return this._last_connected
|
||||||
}
|
}
|
||||||
|
|
||||||
public get config(): ReconnecterConfig {
|
public get config(): ReconnectorConfig {
|
||||||
return { ...this._config }
|
return { ...this._config }
|
||||||
}
|
}
|
||||||
|
|
||||||
public set config(conf: ReconnecterConfig) {
|
public set config(conf: ReconnectorConfig) {
|
||||||
this._config = { ...conf }
|
this._config = { ...conf }
|
||||||
|
|
||||||
if (this._config.maxReconnects > this._total_reconnects) {
|
if (this._config.maxReconnects > this._total_reconnects) {
|
Loading…
x
Reference in New Issue
Block a user