mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
move reconnector to own folder.
This commit is contained in:
parent
5f1cca5ab2
commit
28f256b5b5
@ -5,7 +5,10 @@ import * as EVENT from '../types/events'
|
||||
import { NekoWebSocket } from './websocket'
|
||||
import { NekoWebRTC } from './webrtc'
|
||||
import { Connection, WebRTCStats } from '../types/state'
|
||||
import { Reconnector, ReconnectorAbstract } from '../utils/reconnector'
|
||||
|
||||
import { Reconnector } from './reconnector'
|
||||
import { WebsocketReconnector } from './reconnector/websocket'
|
||||
import { WebrtcReconnector } from './reconnector/webrtc'
|
||||
|
||||
const WEBRTC_RECONN_MAX_LOSS = 25
|
||||
const WEBRTC_RECONN_FAILED_ATTEMPTS = 5
|
||||
@ -14,94 +17,6 @@ export interface NekoConnectionEvents {
|
||||
disconnect: (error?: Error) => void
|
||||
}
|
||||
|
||||
class WebsocketReconnector extends ReconnectorAbstract {
|
||||
private _state: Connection
|
||||
private _websocket: NekoWebSocket
|
||||
|
||||
private _onConnectHandle: () => void
|
||||
private _onDisconnectHandle: (error?: Error) => void
|
||||
|
||||
constructor(state: Connection, websocket: NekoWebSocket) {
|
||||
super()
|
||||
|
||||
this._state = state
|
||||
this._websocket = websocket
|
||||
|
||||
this._onConnectHandle = () => this.emit('connect')
|
||||
this._websocket.on('connected', this._onConnectHandle)
|
||||
|
||||
this._onDisconnectHandle = (error?: Error) => this.emit('disconnect', error)
|
||||
this._websocket.on('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
|
||||
public get connected() {
|
||||
return this._websocket.connected
|
||||
}
|
||||
|
||||
public connect() {
|
||||
let url = this._state.url
|
||||
url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws'
|
||||
|
||||
const token = this._state.token
|
||||
if (token) {
|
||||
url += '?token=' + encodeURIComponent(token)
|
||||
}
|
||||
|
||||
this._websocket.connect(url)
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this._websocket.disconnect()
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this._websocket.off('connected', this._onConnectHandle)
|
||||
this._websocket.off('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
}
|
||||
|
||||
class WebrtcReconnector extends ReconnectorAbstract {
|
||||
private _state: Connection
|
||||
private _websocket: NekoWebSocket
|
||||
private _webrtc: NekoWebRTC
|
||||
|
||||
private _onConnectHandle: () => void
|
||||
private _onDisconnectHandle: (error?: Error) => void
|
||||
|
||||
constructor(state: Connection, websocket: NekoWebSocket, webrtc: NekoWebRTC) {
|
||||
super()
|
||||
|
||||
this._state = state
|
||||
this._websocket = websocket
|
||||
this._webrtc = webrtc
|
||||
|
||||
this._onConnectHandle = () => this.emit('connect')
|
||||
this._webrtc.on('connected', this._onConnectHandle)
|
||||
|
||||
this._onDisconnectHandle = (error?: Error) => this.emit('disconnect', error)
|
||||
this._webrtc.on('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
|
||||
public get connected() {
|
||||
return this._webrtc.connected
|
||||
}
|
||||
|
||||
public connect() {
|
||||
if (this._websocket.connected) {
|
||||
this._websocket.send(EVENT.SIGNAL_REQUEST, { video: this._state.webrtc.video })
|
||||
}
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this._webrtc.disconnect()
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this._webrtc.off('connected', this._onConnectHandle)
|
||||
this._webrtc.off('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
}
|
||||
|
||||
export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
||||
private _state: Connection
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import EventEmitter from 'eventemitter3'
|
||||
import { ReconnectorConfig } from '../types/reconnector'
|
||||
|
||||
import { ReconnectorConfig } from '../../types/reconnector'
|
||||
|
||||
export interface ReconnectorAbstractEvents {
|
||||
connect: () => void
|
49
src/component/internal/reconnector/webrtc.ts
Normal file
49
src/component/internal/reconnector/webrtc.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import * as EVENT from '../../types/events'
|
||||
import { Connection } from '../../types/state'
|
||||
|
||||
import { NekoWebSocket } from '../websocket'
|
||||
import { NekoWebRTC } from '../webrtc'
|
||||
|
||||
import { ReconnectorAbstract } from '.'
|
||||
|
||||
export class WebrtcReconnector extends ReconnectorAbstract {
|
||||
private _state: Connection
|
||||
private _websocket: NekoWebSocket
|
||||
private _webrtc: NekoWebRTC
|
||||
|
||||
private _onConnectHandle: () => void
|
||||
private _onDisconnectHandle: (error?: Error) => void
|
||||
|
||||
constructor(state: Connection, websocket: NekoWebSocket, webrtc: NekoWebRTC) {
|
||||
super()
|
||||
|
||||
this._state = state
|
||||
this._websocket = websocket
|
||||
this._webrtc = webrtc
|
||||
|
||||
this._onConnectHandle = () => this.emit('connect')
|
||||
this._webrtc.on('connected', this._onConnectHandle)
|
||||
|
||||
this._onDisconnectHandle = (error?: Error) => this.emit('disconnect', error)
|
||||
this._webrtc.on('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
|
||||
public get connected() {
|
||||
return this._webrtc.connected
|
||||
}
|
||||
|
||||
public connect() {
|
||||
if (this._websocket.connected) {
|
||||
this._websocket.send(EVENT.SIGNAL_REQUEST, { video: this._state.webrtc.video })
|
||||
}
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this._webrtc.disconnect()
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this._webrtc.off('connected', this._onConnectHandle)
|
||||
this._webrtc.off('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
}
|
51
src/component/internal/reconnector/websocket.ts
Normal file
51
src/component/internal/reconnector/websocket.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { Connection } from '../../types/state'
|
||||
|
||||
import { NekoWebSocket } from '../websocket'
|
||||
|
||||
import { ReconnectorAbstract } from '.'
|
||||
|
||||
export class WebsocketReconnector extends ReconnectorAbstract {
|
||||
private _state: Connection
|
||||
private _websocket: NekoWebSocket
|
||||
|
||||
private _onConnectHandle: () => void
|
||||
private _onDisconnectHandle: (error?: Error) => void
|
||||
|
||||
constructor(state: Connection, websocket: NekoWebSocket) {
|
||||
super()
|
||||
|
||||
this._state = state
|
||||
this._websocket = websocket
|
||||
|
||||
this._onConnectHandle = () => this.emit('connect')
|
||||
this._websocket.on('connected', this._onConnectHandle)
|
||||
|
||||
this._onDisconnectHandle = (error?: Error) => this.emit('disconnect', error)
|
||||
this._websocket.on('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
|
||||
public get connected() {
|
||||
return this._websocket.connected
|
||||
}
|
||||
|
||||
public connect() {
|
||||
let url = this._state.url
|
||||
url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws'
|
||||
|
||||
const token = this._state.token
|
||||
if (token) {
|
||||
url += '?token=' + encodeURIComponent(token)
|
||||
}
|
||||
|
||||
this._websocket.connect(url)
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this._websocket.disconnect()
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
this._websocket.off('connected', this._onConnectHandle)
|
||||
this._websocket.off('disconnected', this._onDisconnectHandle)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user