From e0de57fc7082c8dffa3716aa224064819809b4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 18 Jun 2021 00:31:03 +0200 Subject: [PATCH] move websocket to connection. --- src/component/internal/connection.ts | 67 ++++++----------- src/component/internal/messages.ts | 33 ++++++--- src/component/main.vue | 104 +++++++++++---------------- 3 files changed, 86 insertions(+), 118 deletions(-) diff --git a/src/component/internal/connection.ts b/src/component/internal/connection.ts index 3ea0882b..f4a45b18 100644 --- a/src/component/internal/connection.ts +++ b/src/component/internal/connection.ts @@ -10,61 +10,40 @@ export interface NekoConnectionEvents { } export class NekoConnection extends EventEmitter { - staysConnected = false + private _url: string + private _token: string - websocket = new NekoWebSocket() - webrtc = new NekoWebRTC() + public websocket = new NekoWebSocket() + public webrtc = new NekoWebRTC() constructor() { super() - // - // websocket events - // + this._url = '' + this._token = '' + } - 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 setUrl(url: string) { + this._url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws' + } + public setToken(token: string) { + this._token = token } public async connect(): Promise { - this.staysConnected = true + 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 }) } - public async disconnect(): Promise { - this.staysConnected = false + public disconnect() { + this.websocket.disconnect() } } diff --git a/src/component/internal/messages.ts b/src/component/internal/messages.ts index ae66a586..69342b5b 100644 --- a/src/component/internal/messages.ts +++ b/src/component/internal/messages.ts @@ -4,7 +4,7 @@ import * as message from '../types/messages' import EventEmitter from 'eventemitter3' import { Logger } from '../utils/logger' -import { NekoWebSocket } from './websocket' +import { NekoConnection } from './connection' import NekoState from '../types/state' export interface NekoEvents { @@ -42,18 +42,18 @@ export interface NekoEvents { } export class NekoMessages extends EventEmitter { - private _websocket: NekoWebSocket + private _connection: NekoConnection private _state: NekoState private _log: Logger - constructor(websocket: NekoWebSocket, state: NekoState) { + constructor(connection: NekoConnection, state: NekoState) { super() this._log = new Logger('messages') this._state = state - this._websocket = websocket + this._connection = connection - this._websocket.on('message', async (event: string, payload: any) => { + this._connection.websocket.on('message', async (event: string, payload: any) => { // @ts-ignore if (typeof this[event] === 'function') { // @ts-ignore @@ -62,6 +62,11 @@ export class NekoMessages extends EventEmitter { this._log.warn(`unhandled websocket event '${event}':`, payload) } }) + + this._connection.webrtc.on('candidate', (candidate: RTCIceCandidateInit) => { + this._connection.websocket.send(EVENT.SIGNAL_CANDIDATE, candidate) + this.emit('connection.webrtc.sdp.candidate', 'local', candidate) + }) } ///////////////////////////// @@ -101,7 +106,7 @@ export class NekoMessages extends EventEmitter { protected [EVENT.SYSTEM_DISCONNECT]({ message }: message.SystemDisconnect) { this._log.debug('EVENT.SYSTEM_DISCONNECT') - this._websocket.disconnect(new Error(message)) + this._connection.disconnect() this.emit('connection.disconnect', message) } @@ -109,20 +114,26 @@ export class NekoMessages extends EventEmitter { // Signal Events ///////////////////////////// - protected [EVENT.SIGNAL_PROVIDE]({ event, sdp, video }: message.SignalProvide) { + protected async [EVENT.SIGNAL_PROVIDE]({ sdp: remoteSdp, video, iceservers }: message.SignalProvide) { this._log.debug('EVENT.SIGNAL_PROVIDE') + this.emit('connection.webrtc.sdp', 'remote', remoteSdp) + + const localSdp = await this._connection.webrtc.connect(remoteSdp, iceservers) + this._connection.websocket.send(EVENT.SIGNAL_ANSWER, { + sdp: localSdp, + }) + + this.emit('connection.webrtc.sdp', 'local', localSdp) Vue.set(this._state.connection.webrtc, 'video', video) - // TODO: Handle. - this.emit('connection.webrtc.sdp', 'remote', sdp) } protected [EVENT.SIGNAL_CANDIDATE]({ event, ...candidate }: message.SignalCandidate) { this._log.debug('EVENT.SIGNAL_CANDIDATE') - // TODO: Handle. + this._connection.webrtc.setCandidate(candidate) this.emit('connection.webrtc.sdp.candidate', 'remote', candidate) } - protected [EVENT.SIGNAL_VIDEO]({ event, video }: message.SignalVideo) { + protected [EVENT.SIGNAL_VIDEO]({ video }: message.SignalVideo) { this._log.debug('EVENT.SIGNAL_VIDEO') Vue.set(this._state.connection.webrtc, 'video', video) } diff --git a/src/component/main.vue b/src/component/main.vue index 5e48c5a0..36a2e7e3 100644 --- a/src/component/main.vue +++ b/src/component/main.vue @@ -3,7 +3,7 @@