From e99047963cd86eddac48bb6b2cc5261216a2731b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 5 Nov 2020 18:06:55 +0100 Subject: [PATCH] webrtc & websocket simple implementation. --- src/Neko.vue | 43 ++++++++++++++++++++++++++++++++++++--- src/internal/webrtc.ts | 20 ++++++++---------- src/internal/websocket.ts | 20 +++++------------- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/Neko.vue b/src/Neko.vue index 601c2a84..c58dcad4 100644 --- a/src/Neko.vue +++ b/src/Neko.vue @@ -1,18 +1,55 @@ diff --git a/src/internal/webrtc.ts b/src/internal/webrtc.ts index c435b935..9b9efc82 100644 --- a/src/internal/webrtc.ts +++ b/src/internal/webrtc.ts @@ -15,7 +15,7 @@ export interface NekoWebRTCEvents { track: (event: RTCTrackEvent) => void } -export abstract class NekoWebRTC extends EventEmitter { +export class NekoWebRTC extends EventEmitter { private _peer?: RTCPeerConnection private _channel?: RTCDataChannel private _state: RTCIceConnectionState = 'disconnected' @@ -23,7 +23,7 @@ export abstract class NekoWebRTC extends EventEmitter { constructor() { super() - + this._log = new Logger('webrtc') } @@ -37,11 +37,11 @@ export abstract class NekoWebRTC extends EventEmitter { public async connect(sdp: string, lite: boolean, servers: string[]): Promise { this._log.debug(`creating peer`) - + if (!this.supported) { throw new Error('browser does not support webrtc') } - + if (this.connected) { throw new Error('attempting to create peer while connected') } @@ -87,7 +87,7 @@ export abstract class NekoWebRTC extends EventEmitter { this._peer.addTransceiver('video', { direction: 'recvonly' }) this._channel = this._peer.createDataChannel('data') - this._channel.onerror = this.onError.bind(this) + this._channel.onerror = this.onDisconnected.bind(this, new Error('peer data channel error')) this._channel.onmessage = this.onData.bind(this) this._channel.onclose = this.onDisconnected.bind(this, new Error('peer data channel closed')) @@ -175,17 +175,13 @@ export abstract class NekoWebRTC extends EventEmitter { private onTrack(event: RTCTrackEvent) { this._log.debug(`received ${event.track.kind} track from peer: ${event.track.id}`, event) const stream = event.streams[0] - + if (!stream) { this._log.warn(`no stream provided for track ${event.track.id}(${event.track.label})`) return } - - this.emit('track', event) - } - private onError(event: Event) { - this._log.error((event as ErrorEvent).error) + this.emit('track', event) } private onConnected() { @@ -201,7 +197,7 @@ export abstract class NekoWebRTC extends EventEmitter { private onDisconnected(reason?: Error) { this.disconnect() - this._log.debug(`disconnected:`, reason) + this._log.debug(`disconnected:`, reason?.message) this.emit('disconnected', reason) } } diff --git a/src/internal/websocket.ts b/src/internal/websocket.ts index 51e439f4..aa60edfb 100644 --- a/src/internal/websocket.ts +++ b/src/internal/websocket.ts @@ -7,6 +7,7 @@ export interface NekoWebSocketEvents { connecting: () => void connected: () => void disconnected: (error?: Error) => void + message: (event: string, payload: any) => void } export class NekoWebSocket extends EventEmitter { @@ -36,7 +37,7 @@ export class NekoWebSocket extends EventEmitter { this._ws.onopen = this.onConnected.bind(this) this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed')) - this._ws.onerror = this.onError.bind(this) + this._ws.onerror = this.onDisconnected.bind(this, new Error('websocket error')) this._ws.onmessage = this.onMessage.bind(this) this._timeout = setTimeout(this.onTimeout.bind(this), timeout) @@ -68,20 +69,9 @@ export class NekoWebSocket extends EventEmitter { private onMessage(e: MessageEvent) { const { event, ...payload } = JSON.parse(e.data) + this._log.debug(`received websocket event ${event} ${payload ? `with payload: ` : ''}`, payload) - - // @ts-ignore - if (typeof this[event] === 'function') { - // @ts-ignore - // TODO: REFACTOR - this[event](payload) - } else { - this._log.warn(`unhandled websocket event '${event}':`, payload) - } - } - - private onError(event: Event) { - this._log.error((event as ErrorEvent).error) + this.emit('message', event, payload) } private onConnected() { @@ -104,7 +94,7 @@ export class NekoWebSocket extends EventEmitter { } private onDisconnected(reason?: Error) { - this._log.debug(`disconnected:`, reason) + this._log.debug(`disconnected:`, reason?.message) this.disconnect() this.emit('disconnected', reason)