mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
connection: add destroy handles.
This commit is contained in:
parent
f8b1177178
commit
610805867b
@ -30,6 +30,12 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
|||||||
webrtc: Reconnector
|
webrtc: Reconnector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _onConnectHandle: () => void
|
||||||
|
private _onDisconnectHandle: () => void
|
||||||
|
private _onCloseHandle: (error?: Error) => void
|
||||||
|
|
||||||
|
private _webrtcCongestionControlHandle: (stats: WebRTCStats) => void
|
||||||
|
|
||||||
constructor(state: Connection) {
|
constructor(state: Connection) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
@ -39,43 +45,47 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
|||||||
webrtc: new Reconnector(new WebrtcReconnector(state, this.websocket, this.webrtc), state.webrtc.config),
|
webrtc: new Reconnector(new WebrtcReconnector(state, this.websocket, this.webrtc), state.webrtc.config),
|
||||||
}
|
}
|
||||||
|
|
||||||
// websocket
|
this._onConnectHandle = () => {
|
||||||
this._reconnector.websocket.on('connect', () => {
|
if (this._state.status !== 'connected' && this.websocket.connected && this.webrtc.connected) {
|
||||||
if (this.websocket.connected && this.webrtc.connected) {
|
|
||||||
Vue.set(this._state, 'status', 'connected')
|
Vue.set(this._state, 'status', 'connected')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.webrtc.connected) {
|
if (this._state.type !== 'webrtc' && this.webrtc.connected) {
|
||||||
|
Vue.set(this._state, 'type', 'webrtc')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.websocket.connected && !this.webrtc.connected) {
|
||||||
this._reconnector.webrtc.connect()
|
this._reconnector.webrtc.connect()
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
this._reconnector.websocket.on('disconnect', () => {
|
|
||||||
if (this._state.status === 'connected' && this.activated) {
|
|
||||||
Vue.set(this._state, 'status', 'connecting')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
this._reconnector.websocket.on('close', this.close.bind(this))
|
|
||||||
|
|
||||||
// webrtc
|
this._onDisconnectHandle = () => {
|
||||||
this._reconnector.webrtc.on('connect', () => {
|
|
||||||
if (this.websocket.connected && this.webrtc.connected) {
|
|
||||||
Vue.set(this._state, 'status', 'connected')
|
|
||||||
}
|
|
||||||
|
|
||||||
Vue.set(this._state, 'type', 'webrtc')
|
|
||||||
})
|
|
||||||
this._reconnector.webrtc.on('disconnect', () => {
|
|
||||||
if (this._state.status === 'connected' && this.activated) {
|
if (this._state.status === 'connected' && this.activated) {
|
||||||
Vue.set(this._state, 'status', 'connecting')
|
Vue.set(this._state, 'status', 'connecting')
|
||||||
}
|
}
|
||||||
|
|
||||||
Vue.set(this._state, 'type', 'fallback')
|
if (this._state.type !== 'fallback' && !this.webrtc.connected) {
|
||||||
|
Vue.set(this._state, 'type', 'fallback')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this._onCloseHandle = this.close.bind(this)
|
||||||
|
|
||||||
|
// bind events to all reconnecters
|
||||||
|
Object.values(this._reconnector).forEach((r) => {
|
||||||
|
r.on('connect', this._onConnectHandle)
|
||||||
|
r.on('disconnect', this._onDisconnectHandle)
|
||||||
|
r.on('close', this._onCloseHandle)
|
||||||
})
|
})
|
||||||
this._reconnector.webrtc.on('close', this.close.bind(this))
|
|
||||||
|
//
|
||||||
|
// TODO: Use server side congestion control.
|
||||||
|
//
|
||||||
|
|
||||||
let webrtcCongestion: number = 0
|
let webrtcCongestion: number = 0
|
||||||
let webrtcFallbackTimeout: number
|
let webrtcFallbackTimeout: number
|
||||||
this.webrtc.on('stats', (stats: WebRTCStats) => {
|
|
||||||
|
this._webrtcCongestionControlHandle = (stats: WebRTCStats) => {
|
||||||
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
|
||||||
@ -127,7 +137,9 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
|||||||
// try to reconnect webrtc
|
// try to reconnect webrtc
|
||||||
this._reconnector.webrtc.reconnect()
|
this._reconnector.webrtc.reconnect()
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
this.webrtc.on('stats', this._webrtcCongestionControlHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
public get activated() {
|
public get activated() {
|
||||||
@ -174,6 +186,16 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public destroy() {
|
public destroy() {
|
||||||
|
// TODO: Use server side congestion control.
|
||||||
|
this.webrtc.off('stats', this._webrtcCongestionControlHandle)
|
||||||
|
|
||||||
|
// unbind events from all reconnecters
|
||||||
|
Object.values(this._reconnector).forEach((r) => {
|
||||||
|
r.off('connect', this._onConnectHandle)
|
||||||
|
r.off('disconnect', this._onDisconnectHandle)
|
||||||
|
r.off('close', this._onCloseHandle)
|
||||||
|
})
|
||||||
|
|
||||||
// destroy all reconnecters
|
// destroy all reconnecters
|
||||||
Object.values(this._reconnector).forEach((r) => r.destroy())
|
Object.values(this._reconnector).forEach((r) => r.destroy())
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user