From cab53d7d1755dd79f3a6c24707afefbe1c6fc752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 7 Dec 2020 18:46:29 +0100 Subject: [PATCH] webrtc connect / disconnect functions. --- src/component/internal/webrtc.ts | 7 +++++-- src/component/main.vue | 34 +++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/component/internal/webrtc.ts b/src/component/internal/webrtc.ts index dc2de4e2..a19fd6a3 100644 --- a/src/component/internal/webrtc.ts +++ b/src/component/internal/webrtc.ts @@ -73,11 +73,14 @@ export class NekoWebRTC extends EventEmitter { this._log.debug(`peer ice connection state changed: ${this._peer!.iceConnectionState}`) switch (this._state) { + case 'disconnected': + this.onDisconnected(new Error('peer disconnected')) + break case 'failed': this.onDisconnected(new Error('peer failed')) break - case 'disconnected': - this.onDisconnected(new Error('peer disconnected')) + case 'closed': + this.onDisconnected(new Error('peer closed')) break } } diff --git a/src/component/main.vue b/src/component/main.vue index f13ede39..53fcbbcb 100644 --- a/src/component/main.vue +++ b/src/component/main.vue @@ -6,7 +6,7 @@ :webrtc="webrtc" :screenWidth="state.screen.size.width" :screenHeight="state.screen.size.height" - :isControling="controlling" + :isControling="controlling && watching" :scrollSensitivity="state.control.scroll.sensitivity" :scrollInvert="state.control.scroll.inverse" :implicitControl="state.control.implicit_hosting && state.members[state.member_id].profile.can_host" @@ -113,7 +113,11 @@ } as NekoState public get connected() { - return this.state.connection.websocket == 'connected' && this.state.connection.webrtc == 'connected' + return this.state.connection.websocket == 'connected' + } + + public get watching() { + return this.state.connection.webrtc == 'connected' } public get controlling() { @@ -153,6 +157,30 @@ this.api.disconnect() } + public webrtcConnect() { + if (!this.connected) { + throw new Error('client not connected to websocket') + } + + if (this.watching) { + throw new Error('client already connected to webrtc') + } + + this.websocket.send('signal/request') + } + + public webrtcDisconnect() { + if (!this.connected) { + throw new Error('client not connected to websocket') + } + + if (!this.watching) { + throw new Error('client not connected to webrtc') + } + + this.webrtc.disconnect() + } + public play() { this._video.play() } @@ -290,9 +318,9 @@ this.events.emit('internal.websocket', 'connecting') }) this.websocket.on('connected', () => { - this.websocket.send('signal/request') Vue.set(this.state.connection, 'websocket', 'connected') this.events.emit('internal.websocket', 'connected') + this.webrtcConnect() }) this.websocket.on('disconnected', () => { Vue.set(this.state.connection, 'websocket', 'disconnected')