2021-06-19 09:15:29 +12:00
|
|
|
import Vue from 'vue'
|
2021-06-17 19:22:02 +12:00
|
|
|
import EventEmitter from 'eventemitter3'
|
|
|
|
|
|
|
|
import { NekoWebSocket } from './websocket'
|
|
|
|
import { NekoWebRTC, WebRTCStats } from './webrtc'
|
2021-06-19 09:15:29 +12:00
|
|
|
import { Connection } from '../types/state'
|
2021-06-17 19:22:02 +12:00
|
|
|
|
|
|
|
export interface NekoConnectionEvents {
|
2021-06-20 04:44:43 +12:00
|
|
|
disconnect: (error?: Error) => void
|
2021-06-17 19:22:02 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
|
2021-06-18 10:31:03 +12:00
|
|
|
private _url: string
|
|
|
|
private _token: string
|
2021-06-19 09:15:29 +12:00
|
|
|
private _state: Connection
|
2021-06-17 19:22:02 +12:00
|
|
|
|
2021-06-18 10:31:03 +12:00
|
|
|
public websocket = new NekoWebSocket()
|
|
|
|
public webrtc = new NekoWebRTC()
|
2021-06-17 19:22:02 +12:00
|
|
|
|
2021-06-19 09:15:29 +12:00
|
|
|
constructor(state: Connection) {
|
2021-06-17 19:22:02 +12:00
|
|
|
super()
|
|
|
|
|
2021-06-18 10:31:03 +12:00
|
|
|
this._url = ''
|
|
|
|
this._token = ''
|
2021-06-19 09:15:29 +12:00
|
|
|
this._state = state
|
|
|
|
|
|
|
|
// initial state
|
|
|
|
Vue.set(this._state, 'type', 'webrtc')
|
2021-06-20 04:36:48 +12:00
|
|
|
|
|
|
|
let webSocketStatus = 'disconnected'
|
|
|
|
let webRTCStatus = 'disconnected'
|
2021-06-19 09:15:29 +12:00
|
|
|
|
|
|
|
// websocket
|
|
|
|
this.websocket.on('connecting', () => {
|
2021-06-20 04:36:48 +12:00
|
|
|
webSocketStatus = 'connecting'
|
|
|
|
if (this._state.status !== 'connecting') {
|
|
|
|
Vue.set(this._state, 'status', 'connecting')
|
|
|
|
}
|
2021-06-19 09:15:29 +12:00
|
|
|
})
|
|
|
|
this.websocket.on('connected', () => {
|
2021-06-20 04:36:48 +12:00
|
|
|
webSocketStatus = 'connected'
|
|
|
|
if (webSocketStatus == 'connected' && webRTCStatus == 'connected') {
|
|
|
|
Vue.set(this._state, 'status', 'connected')
|
|
|
|
}
|
2021-06-19 09:15:29 +12:00
|
|
|
})
|
|
|
|
this.websocket.on('disconnected', () => {
|
2021-06-20 04:36:48 +12:00
|
|
|
webSocketStatus = 'disconnected'
|
|
|
|
if (this._state.status !== 'disconnected') {
|
|
|
|
Vue.set(this._state, 'status', 'disconnected')
|
|
|
|
}
|
2021-06-19 09:15:29 +12:00
|
|
|
})
|
|
|
|
|
|
|
|
// webrtc
|
|
|
|
this.webrtc.on('connecting', () => {
|
2021-06-20 04:36:48 +12:00
|
|
|
webRTCStatus = 'connecting'
|
|
|
|
if (this._state.status !== 'connecting') {
|
|
|
|
Vue.set(this._state, 'status', 'connecting')
|
|
|
|
}
|
2021-06-19 09:15:29 +12:00
|
|
|
})
|
|
|
|
this.webrtc.on('connected', () => {
|
2021-06-20 04:36:48 +12:00
|
|
|
webRTCStatus = 'connected'
|
|
|
|
if (webSocketStatus == 'connected' && webRTCStatus == 'connected') {
|
|
|
|
Vue.set(this._state, 'status', 'connected')
|
|
|
|
}
|
2021-06-19 09:15:29 +12:00
|
|
|
})
|
|
|
|
this.webrtc.on('disconnected', () => {
|
2021-06-20 04:36:48 +12:00
|
|
|
webRTCStatus = 'disconnected'
|
|
|
|
if (this._state.status !== 'disconnected') {
|
|
|
|
Vue.set(this._state, 'status', 'disconnected')
|
|
|
|
}
|
2021-06-19 09:15:29 +12:00
|
|
|
})
|
2021-06-18 10:31:03 +12:00
|
|
|
}
|
2021-06-17 19:22:02 +12:00
|
|
|
|
2021-06-18 10:31:03 +12:00
|
|
|
public setUrl(url: string) {
|
|
|
|
this._url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws'
|
|
|
|
}
|
2021-06-17 19:22:02 +12:00
|
|
|
|
2021-06-18 10:31:03 +12:00
|
|
|
public setToken(token: string) {
|
|
|
|
this._token = token
|
2021-06-17 19:22:02 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public async connect(): Promise<void> {
|
2021-06-18 10:31:03 +12:00
|
|
|
let url = this._url
|
|
|
|
if (this._token) {
|
|
|
|
url += '?token=' + encodeURIComponent(this._token)
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.websocket.connect(url)
|
|
|
|
|
|
|
|
// TODO: connect to WebRTC
|
|
|
|
//this.websocket.send(EVENT.SIGNAL_REQUEST, { video: video })
|
2021-06-17 19:22:02 +12:00
|
|
|
}
|
|
|
|
|
2021-06-18 10:31:03 +12:00
|
|
|
public disconnect() {
|
2021-06-20 04:44:43 +12:00
|
|
|
this.webrtc.disconnect()
|
2021-06-18 10:31:03 +12:00
|
|
|
this.websocket.disconnect()
|
2021-06-20 04:36:48 +12:00
|
|
|
Vue.set(this._state, 'status', 'disconnected')
|
2021-06-20 04:44:43 +12:00
|
|
|
this.emit('disconnect')
|
2021-06-17 19:22:02 +12:00
|
|
|
}
|
|
|
|
}
|