neko/src/component/internal/connection.ts

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-06-17 19:22:02 +12:00
import EventEmitter from 'eventemitter3'
import { NekoWebSocket } from './websocket'
import { NekoWebRTC, WebRTCStats } from './webrtc'
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-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
constructor() {
super()
2021-06-18 10:31:03 +12:00
this._url = ''
this._token = ''
}
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
}
}