add reconnecter config.

This commit is contained in:
Miroslav Šedivý 2021-07-28 20:41:54 +02:00
parent 610805867b
commit 0d32a0592c
5 changed files with 32 additions and 23 deletions

View File

@ -89,7 +89,7 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
Vue.set(this._state.webrtc, 'stats', stats) Vue.set(this._state.webrtc, 'stats', stats)
// if automatic quality adjusting is turned off // if automatic quality adjusting is turned off
if (!this._state.webrtc.auto || !this._reconnector.webrtc.isOpen) return if (!this._reconnector.webrtc.isOpen) return
// if there are no or just one quality, no switching can be done // if there are no or just one quality, no switching can be done
if (this._state.webrtc.videos.length <= 1) return if (this._state.webrtc.videos.length <= 1) return
@ -147,6 +147,11 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
return Object.values(this._reconnector).every((r) => r.isOpen) return Object.values(this._reconnector).every((r) => r.isOpen)
} }
public reloadConfigs() {
this._reconnector.websocket.config = this._state.websocket.config
this._reconnector.webrtc.config = this._state.webrtc.config
}
public setVideo(video: string) { public setVideo(video: string) {
if (!this._state.webrtc.videos.includes(video)) { if (!this._state.webrtc.videos.includes(video)) {
throw new Error('video id not found') throw new Error('video id not found')

View File

@ -47,9 +47,9 @@ export class Reconnector extends EventEmitter<ReconnectorEvents> {
this._conn = conn this._conn = conn
this._config = { this._config = {
maxReconnects: 10, max_reconnects: 10,
timeoutMs: 1500, timeout_ms: 1500,
backoffMs: 750, backoff_ms: 750,
...config, ...config,
} }
@ -109,7 +109,7 @@ export class Reconnector extends EventEmitter<ReconnectorEvents> {
public set config(conf: ReconnectorConfig) { public set config(conf: ReconnectorConfig) {
this._config = { ...conf } this._config = { ...conf }
if (this._config.maxReconnects > this._total_reconnects) { if (this._config.max_reconnects > this._total_reconnects) {
this.close(new Error('reconnection config changed')) this.close(new Error('reconnection config changed'))
} }
} }
@ -154,7 +154,7 @@ export class Reconnector extends EventEmitter<ReconnectorEvents> {
} }
this._conn.connect() this._conn.connect()
this._timeout = window.setTimeout(this.onDisconnect.bind(this), this._config.timeoutMs) this._timeout = window.setTimeout(this.onDisconnect.bind(this), this._config.timeout_ms)
} }
public reconnect(): void { public reconnect(): void {
@ -165,8 +165,8 @@ export class Reconnector extends EventEmitter<ReconnectorEvents> {
this._total_reconnects++ this._total_reconnects++
if (this._config.maxReconnects > this._total_reconnects || this._total_reconnects < 0) { if (this._config.max_reconnects > this._total_reconnects || this._total_reconnects < 0) {
this._timeout = window.setTimeout(this.connect.bind(this), this._config.backoffMs) this._timeout = window.setTimeout(this.connect.bind(this), this._config.backoff_ms)
} else { } else {
this.close(new Error('reconnection failed')) this.close(new Error('reconnection failed'))
} }

View File

@ -63,6 +63,7 @@
import { NekoMessages } from './internal/messages' import { NekoMessages } from './internal/messages'
import { register as VideoRegister } from './internal/video' import { register as VideoRegister } from './internal/video'
import { ReconnectorConfig } from './types/reconnector'
import NekoState from './types/state' import NekoState from './types/state'
import Overlay from './overlay.vue' import Overlay from './overlay.vue'
import Screencast from './screencast.vue' import Screencast from './screencast.vue'
@ -109,21 +110,20 @@
status: 'disconnected', status: 'disconnected',
websocket: { websocket: {
config: { config: {
maxReconnects: 15, max_reconnects: 15,
timeoutMs: 5000, timeout_ms: 5000,
backoffMs: 1500, backoff_ms: 1500,
}, },
}, },
webrtc: { webrtc: {
config: { config: {
maxReconnects: 15, max_reconnects: 15,
timeoutMs: 10000, timeout_ms: 10000,
backoffMs: 1500, backoff_ms: 1500,
}, },
stats: null, stats: null,
video: null, video: null,
videos: [], videos: [],
auto: true,
}, },
screencast: false, screencast: false,
type: 'none', type: 'none',
@ -292,6 +292,15 @@
this.connection.close() this.connection.close()
} }
public setReconnectorConfig(type: 'websocket' | 'webrtc', config: ReconnectorConfig) {
if (type != 'websocket' && type != 'webrtc') {
throw new Error('unknown reconnector type')
}
Vue.set(this.state.connection[type], 'config', config)
this.connection.reloadConfigs()
}
public play() { public play() {
this._video.play() this._video.play()
} }
@ -337,10 +346,6 @@
this.connection.setVideo(video) this.connection.setVideo(video)
} }
public setWebRTCAuto(auto: boolean = true) {
Vue.set(this.state.connection.webrtc, 'auto', auto)
}
public sendUnicast(receiver: string, subject: string, body: any) { public sendUnicast(receiver: string, subject: string, body: any) {
this.connection.websocket.send(EVENT.SEND_UNICAST, { receiver, subject, body }) this.connection.websocket.send(EVENT.SEND_UNICAST, { receiver, subject, body })
} }

View File

@ -1,5 +1,5 @@
export interface ReconnectorConfig { export interface ReconnectorConfig {
maxReconnects: number max_reconnects: number
timeoutMs: number timeout_ms: number
backoffMs: number backoff_ms: number
} }

View File

@ -34,7 +34,6 @@ export interface WebRTC {
stats: WebRTCStats | null stats: WebRTCStats | null
video: string | null video: string | null
videos: string[] videos: string[]
auto: boolean
} }
export interface ReconnectorConfig extends reconnecterTypes.ReconnectorConfig {} export interface ReconnectorConfig extends reconnecterTypes.ReconnectorConfig {}