From daff2552a39ae12c853f939c32c325085701179c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 19 Jun 2021 18:44:43 +0200 Subject: [PATCH] add disconnect event. --- src/component/internal/connection.ts | 6 +++--- src/component/main.vue | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/component/internal/connection.ts b/src/component/internal/connection.ts index 6b58489a..27480b6e 100644 --- a/src/component/internal/connection.ts +++ b/src/component/internal/connection.ts @@ -6,9 +6,7 @@ import { NekoWebRTC, WebRTCStats } from './webrtc' import { Connection } from '../types/state' export interface NekoConnectionEvents { - connecting: () => void - connected: () => void - disconnected: (error?: Error) => void + disconnect: (error?: Error) => void } export class NekoConnection extends EventEmitter { @@ -94,7 +92,9 @@ export class NekoConnection extends EventEmitter { } public disconnect() { + this.webrtc.disconnect() this.websocket.disconnect() Vue.set(this._state, 'status', 'disconnected') + this.emit('disconnect') } } diff --git a/src/component/main.vue b/src/component/main.vue index 0fe34a90..e6fd5f00 100644 --- a/src/component/main.vue +++ b/src/component/main.vue @@ -177,7 +177,7 @@ this.connection.setUrl(httpURL) if (this.connected) { - this.disconnect() + this.connection.disconnect() } if (this.state.authenticated) { @@ -229,7 +229,7 @@ } if (this.connected) { - this.disconnect() + this.connection.disconnect() } try { @@ -270,11 +270,7 @@ throw new Error('client is not connected') } - // TODO: Refactor. - this.connection.webrtc.disconnect() - this.connection.disconnect() - this.clear() } public play() { @@ -318,6 +314,7 @@ this.connection.websocket.send(EVENT.SCREEN_SET, { width, height, rate }) } + // TODO: Refactor. public setWebRTCVideo(video: string) { if (!this.state.connection.webrtc.videos.includes(video)) { throw new Error('video id not found') @@ -326,6 +323,7 @@ this.connection.websocket.send(EVENT.SIGNAL_VIDEO, { video: video }) } + // TODO: Refactor. public setWebRTCAuto(auto: boolean = true) { Vue.set(this.state.connection.webrtc, 'auto', auto) } @@ -372,6 +370,10 @@ // video events VideoRegister(this._video, this.state.video) + this.connection.on('disconnect', () => { + this.clear() + }) + // webrtc this.connection.webrtc.on('track', (event: RTCTrackEvent) => { const { track, streams } = event @@ -433,7 +435,7 @@ beforeDestroy() { this.observer.disconnect() - this.disconnect() + this.connection.disconnect() // remove users first interaction event document.removeEventListener('click', this.unmute)