add connection skeleton (WIP).

This commit is contained in:
Miroslav Šedivý 2021-06-17 09:22:02 +02:00
parent 4497a18793
commit af206457ab
2 changed files with 76 additions and 1 deletions

View File

@ -0,0 +1,70 @@
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> {
staysConnected = false
websocket = new NekoWebSocket()
webrtc = new NekoWebRTC()
constructor() {
super()
//
// websocket events
//
this.websocket.on('message', async (event: string, payload: any) => {
})
this.websocket.on('connecting', () => {
})
this.websocket.on('connected', () => {
})
this.websocket.on('disconnected', () => {
})
//
// webrtc events
//
this.webrtc.on('track', (event: RTCTrackEvent) => {
})
this.webrtc.on('candidate', (candidate: RTCIceCandidateInit) => {
})
this.webrtc.on('stats', (stats: WebRTCStats) => {
})
this.webrtc.on('connecting', () => {
})
this.webrtc.on('connected', () => {
})
this.webrtc.on('disconnected', () => {
})
}
public async connect(): Promise<void> {
this.staysConnected = true
}
public async disconnect(): Promise<void> {
this.staysConnected = false
}
}

View File

@ -55,7 +55,6 @@
import { Vue, Component, Ref, Watch, Prop } from 'vue-property-decorator' import { Vue, Component, Ref, Watch, Prop } from 'vue-property-decorator'
import ResizeObserver from 'resize-observer-polyfill' import ResizeObserver from 'resize-observer-polyfill'
import EventEmitter from 'eventemitter3'
import { NekoApi, MembersApi, RoomApi } from './internal/api' import { NekoApi, MembersApi, RoomApi } from './internal/api'
import { NekoWebSocket } from './internal/websocket' import { NekoWebSocket } from './internal/websocket'
@ -257,6 +256,7 @@
} }
} }
// TODO: Refactor.
public async websocketConnect() { public async websocketConnect() {
if (!this.authenticated) { if (!this.authenticated) {
throw new Error('client not authenticated') throw new Error('client not authenticated')
@ -269,6 +269,7 @@
await this.websocket.connect() await this.websocket.connect()
} }
// TODO: Refactor.
public websocketDisconnect() { public websocketDisconnect() {
if (!this.connected) { if (!this.connected) {
throw new Error('client not connected to websocket') throw new Error('client not connected to websocket')
@ -277,6 +278,7 @@
this.websocket.disconnect(new Error('manual action')) this.websocket.disconnect(new Error('manual action'))
} }
// TODO: Refactor.
public webrtcConnect(video?: string) { public webrtcConnect(video?: string) {
if (!this.connected) { if (!this.connected) {
throw new Error('client not connected to websocket') throw new Error('client not connected to websocket')
@ -293,6 +295,7 @@
this.websocket.send(EVENT.SIGNAL_REQUEST, { video: video }) this.websocket.send(EVENT.SIGNAL_REQUEST, { video: video })
} }
// TODO: Refactor.
public webrtcDisconnect() { public webrtcDisconnect() {
if (!this.watching) { if (!this.watching) {
throw new Error('client not connected to webrtc') throw new Error('client not connected to webrtc')
@ -422,6 +425,7 @@
Vue.set(this.state.connection, 'websocket', 'connected') Vue.set(this.state.connection, 'websocket', 'connected')
this.events.emit('connection.websocket', 'connected') this.events.emit('connection.websocket', 'connected')
// TODO: Refactor.
if (!this.watching && this.autoconnect) { if (!this.watching && this.autoconnect) {
this.webrtcConnect() this.webrtcConnect()
} }
@ -516,6 +520,7 @@
} }
} }
// TODO: Refactor.
// periodically reconnect WebRTC // periodically reconnect WebRTC
if (this.connected && !webrtcReconnect) { if (this.connected && !webrtcReconnect) {
webrtcReconnect = setInterval(() => { webrtcReconnect = setInterval(() => {