add disconnect event.

This commit is contained in:
Miroslav Šedivý 2021-06-19 18:44:43 +02:00
parent 1dcce7d949
commit daff2552a3
2 changed files with 12 additions and 10 deletions

View File

@ -6,9 +6,7 @@ import { NekoWebRTC, WebRTCStats } from './webrtc'
import { Connection } from '../types/state' import { Connection } from '../types/state'
export interface NekoConnectionEvents { export interface NekoConnectionEvents {
connecting: () => void disconnect: (error?: Error) => void
connected: () => void
disconnected: (error?: Error) => void
} }
export class NekoConnection extends EventEmitter<NekoConnectionEvents> { export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
@ -94,7 +92,9 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
} }
public disconnect() { public disconnect() {
this.webrtc.disconnect()
this.websocket.disconnect() this.websocket.disconnect()
Vue.set(this._state, 'status', 'disconnected') Vue.set(this._state, 'status', 'disconnected')
this.emit('disconnect')
} }
} }

View File

@ -177,7 +177,7 @@
this.connection.setUrl(httpURL) this.connection.setUrl(httpURL)
if (this.connected) { if (this.connected) {
this.disconnect() this.connection.disconnect()
} }
if (this.state.authenticated) { if (this.state.authenticated) {
@ -229,7 +229,7 @@
} }
if (this.connected) { if (this.connected) {
this.disconnect() this.connection.disconnect()
} }
try { try {
@ -270,11 +270,7 @@
throw new Error('client is not connected') throw new Error('client is not connected')
} }
// TODO: Refactor.
this.connection.webrtc.disconnect()
this.connection.disconnect() this.connection.disconnect()
this.clear()
} }
public play() { public play() {
@ -318,6 +314,7 @@
this.connection.websocket.send(EVENT.SCREEN_SET, { width, height, rate }) this.connection.websocket.send(EVENT.SCREEN_SET, { width, height, rate })
} }
// TODO: Refactor.
public setWebRTCVideo(video: string) { public setWebRTCVideo(video: string) {
if (!this.state.connection.webrtc.videos.includes(video)) { if (!this.state.connection.webrtc.videos.includes(video)) {
throw new Error('video id not found') throw new Error('video id not found')
@ -326,6 +323,7 @@
this.connection.websocket.send(EVENT.SIGNAL_VIDEO, { video: video }) this.connection.websocket.send(EVENT.SIGNAL_VIDEO, { video: video })
} }
// TODO: Refactor.
public setWebRTCAuto(auto: boolean = true) { public setWebRTCAuto(auto: boolean = true) {
Vue.set(this.state.connection.webrtc, 'auto', auto) Vue.set(this.state.connection.webrtc, 'auto', auto)
} }
@ -372,6 +370,10 @@
// video events // video events
VideoRegister(this._video, this.state.video) VideoRegister(this._video, this.state.video)
this.connection.on('disconnect', () => {
this.clear()
})
// webrtc // webrtc
this.connection.webrtc.on('track', (event: RTCTrackEvent) => { this.connection.webrtc.on('track', (event: RTCTrackEvent) => {
const { track, streams } = event const { track, streams } = event
@ -433,7 +435,7 @@
beforeDestroy() { beforeDestroy() {
this.observer.disconnect() this.observer.disconnect()
this.disconnect() this.connection.disconnect()
// remove users first interaction event // remove users first interaction event
document.removeEventListener('click', this.unmute) document.removeEventListener('click', this.unmute)