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 {
|
|
|
|
connecting: () => void
|
|
|
|
connected: () => void
|
|
|
|
disconnected: (error?: Error) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
|
|
|
Vue.set(this._state, 'websocket', this.websocket.supported ? 'disconnected' : 'unavailable')
|
|
|
|
Vue.set(this._state.webrtc, 'status', this.webrtc.supported ? 'disconnected' : 'unavailable')
|
|
|
|
|
|
|
|
// websocket
|
|
|
|
this.websocket.on('connecting', () => {
|
|
|
|
Vue.set(this._state, 'websocket', 'connecting')
|
|
|
|
})
|
|
|
|
this.websocket.on('connected', () => {
|
|
|
|
Vue.set(this._state, 'websocket', 'connected')
|
|
|
|
})
|
|
|
|
this.websocket.on('disconnected', () => {
|
|
|
|
Vue.set(this._state, 'websocket', 'disconnected')
|
|
|
|
})
|
|
|
|
|
|
|
|
// webrtc
|
|
|
|
this.webrtc.on('connecting', () => {
|
|
|
|
Vue.set(this._state.webrtc, 'status', 'connecting')
|
|
|
|
})
|
|
|
|
this.webrtc.on('connected', () => {
|
|
|
|
Vue.set(this._state.webrtc, 'status', 'connected')
|
|
|
|
})
|
|
|
|
this.webrtc.on('disconnected', () => {
|
|
|
|
Vue.set(this._state.webrtc, 'status', 'disconnected')
|
|
|
|
})
|
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() {
|
|
|
|
this.websocket.disconnect()
|
2021-06-17 19:22:02 +12:00
|
|
|
}
|
|
|
|
}
|