neko/src/component/internal/messages.ts

237 lines
7.6 KiB
TypeScript
Raw Normal View History

2020-12-30 08:50:49 +13:00
import Vue from 'vue'
import * as EVENT from '../types/events'
import * as message from '../types/messages'
import EventEmitter from 'eventemitter3'
2021-01-16 05:17:49 +13:00
import { Logger } from '../utils/logger'
2020-12-30 08:50:49 +13:00
import { NekoWebSocket } from './websocket'
import NekoState from '../types/state'
export interface NekoEvents {
2021-02-01 02:25:17 +13:00
// connection events
['connection.websocket']: (state: 'connected' | 'connecting' | 'disconnected') => void
['connection.webrtc']: (state: 'connected' | 'connecting' | 'disconnected') => void
2021-04-19 08:49:41 +12:00
['connection.webrtc.sdp']: (type: 'local' | 'remote', data: string) => void
['connection.webrtc.sdp.candidate']: (type: 'local' | 'remote', data: RTCIceCandidateInit) => void
2021-02-01 02:25:17 +13:00
['connection.disconnect']: (message: string) => void
// drag and drop events
2021-01-09 09:36:59 +13:00
['upload.drop.started']: () => void
['upload.drop.progress']: (progressEvent: ProgressEvent) => void
['upload.drop.finished']: (error: Error | null) => void
2021-02-01 02:25:17 +13:00
// upload dialog events
['upload.dialog.requested']: () => void
['upload.dialog.overlay']: (id: string) => void
['upload.dialog.closed']: () => void
// custom messages events
['receive.unicast']: (sender: string, subject: string, body: any) => void
['receive.broadcast']: (sender: string, subject: string, body: any) => void
2021-03-14 12:08:52 +13:00
// session events
['session.created']: (id: string) => void
['session.deleted']: (id: string) => void
['session.updated']: (id: string) => void
2021-02-01 02:25:17 +13:00
// room events
['room.control.host']: (hasHost: boolean, hostID: string | undefined) => void
['room.screen.updated']: (width: number, height: number, rate: number) => void
['room.clipboard.updated']: (text: string) => void
['room.broadcast.status']: (isActive: boolean, url: string | undefined) => void
2020-12-30 08:50:49 +13:00
}
export class NekoMessages extends EventEmitter<NekoEvents> {
private websocket: NekoWebSocket
2021-01-16 05:17:49 +13:00
private state: NekoState
private _log: Logger
2020-12-30 08:50:49 +13:00
constructor(websocket: NekoWebSocket, state: NekoState) {
super()
2021-01-16 05:17:49 +13:00
this._log = new Logger('messages')
2020-12-30 08:50:49 +13:00
this.state = state
this.websocket = websocket
this.websocket.on('message', async (event: string, payload: any) => {
2020-12-30 08:50:49 +13:00
// @ts-ignore
if (typeof this[event] === 'function') {
// @ts-ignore
this[event](payload)
} else {
2021-01-16 05:17:49 +13:00
this._log.warn(`unhandled websocket event '${event}':`, payload)
2020-12-30 08:50:49 +13:00
}
})
}
/////////////////////////////
// System Events
/////////////////////////////
protected [EVENT.SYSTEM_INIT](conf: message.SystemInit) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.SYSTEM_INIT')
2021-03-14 12:08:52 +13:00
Vue.set(this.state, 'session_id', conf.session_id)
2020-12-30 08:50:49 +13:00
Vue.set(this.state.control, 'implicit_hosting', conf.implicit_hosting)
2021-03-14 12:08:52 +13:00
for (const id in conf.sessions) {
this[EVENT.SESSION_CREATED](conf.sessions[id])
2020-12-30 08:50:49 +13:00
}
this[EVENT.SCREEN_UPDATED](conf.screen_size)
this[EVENT.CONTROL_HOST](conf.control_host)
}
protected [EVENT.SYSTEM_ADMIN]({ screen_sizes_list, broadcast_status }: message.SystemAdmin) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.SYSTEM_ADMIN')
2020-12-30 08:50:49 +13:00
const list = screen_sizes_list.sort((a, b) => {
if (b.width === a.width && b.height == a.height) {
return b.rate - a.rate
} else if (b.width === a.width) {
return b.height - a.height
}
return b.width - a.width
})
Vue.set(this.state.screen, 'configurations', list)
this[EVENT.BORADCAST_STATUS](broadcast_status)
}
protected [EVENT.SYSTEM_DISCONNECT]({ message }: message.SystemDisconnect) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.SYSTEM_DISCONNECT')
this.websocket.disconnect(new Error(message))
2021-02-01 02:25:17 +13:00
this.emit('connection.disconnect', message)
2020-12-30 08:50:49 +13:00
}
/////////////////////////////
// Signal Events
/////////////////////////////
2021-04-19 08:49:41 +12:00
protected [EVENT.SIGNAL_PROVIDE]({ event, sdp, video, videos }: message.SignalProvide) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.SIGNAL_PROVIDE')
2021-02-08 05:28:15 +13:00
Vue.set(this.state.connection.webrtc, 'video', video)
Vue.set(this.state.connection.webrtc, 'videos', videos)
2020-12-30 08:50:49 +13:00
// TODO: Handle.
2021-04-19 08:49:41 +12:00
this.emit('connection.webrtc.sdp', 'remote', sdp)
2020-12-30 08:50:49 +13:00
}
2021-02-03 08:27:23 +13:00
protected [EVENT.SIGNAL_CANDIDATE]({ event, ...candidate }: message.SignalCandidate) {
this._log.debug('EVENT.SIGNAL_CANDIDATE')
// TODO: Handle.
2021-04-19 08:49:41 +12:00
this.emit('connection.webrtc.sdp.candidate', 'remote', candidate)
2021-02-03 08:27:23 +13:00
}
2021-02-08 05:28:15 +13:00
protected [EVENT.SIGNAL_VIDEO]({ event, video }: message.SignalVideo) {
this._log.debug('EVENT.SIGNAL_VIDEO')
Vue.set(this.state.connection.webrtc, 'video', video)
}
2020-12-30 08:50:49 +13:00
/////////////////////////////
2021-03-14 12:08:52 +13:00
// Session Events
2020-12-30 08:50:49 +13:00
/////////////////////////////
2021-03-14 12:08:52 +13:00
protected [EVENT.SESSION_CREATED]({ id, ...data }: message.SessionData) {
2021-03-14 11:48:16 +13:00
this._log.debug('EVENT.SESSION_CREATED', id)
2021-03-14 12:08:52 +13:00
Vue.set(this.state.sessions, id, data)
this.emit('session.created', id)
2020-12-30 08:50:49 +13:00
}
2021-03-14 12:08:52 +13:00
protected [EVENT.SESSION_DELETED]({ id }: message.SessionID) {
2021-03-14 11:48:16 +13:00
this._log.debug('EVENT.SESSION_DELETED', id)
2021-03-14 12:08:52 +13:00
Vue.delete(this.state.sessions, id)
this.emit('session.deleted', id)
2020-12-30 08:50:49 +13:00
}
2021-03-14 11:48:16 +13:00
protected [EVENT.SESSION_PROFILE]({ id, ...profile }: message.MemberProfile) {
this._log.debug('EVENT.SESSION_PROFILE', id)
2021-03-14 12:08:52 +13:00
Vue.set(this.state.sessions[id], 'profile', profile)
this.emit('session.updated', id)
2020-12-30 08:50:49 +13:00
}
2021-03-14 11:48:16 +13:00
protected [EVENT.SESSION_STATE]({ id, ...state }: message.SessionState) {
this._log.debug('EVENT.SESSION_STATE', id)
2021-03-14 12:08:52 +13:00
Vue.set(this.state.sessions[id], 'state', state)
this.emit('session.updated', id)
2020-12-30 08:50:49 +13:00
}
/////////////////////////////
// Control Events
/////////////////////////////
protected [EVENT.CONTROL_HOST]({ has_host, host_id }: message.ControlHost) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.CONTROL_HOST')
2020-12-30 08:50:49 +13:00
if (has_host && host_id) {
Vue.set(this.state.control, 'host_id', host_id)
} else {
Vue.set(this.state.control, 'host_id', null)
}
2021-02-01 02:25:17 +13:00
this.emit('room.control.host', has_host, host_id)
2020-12-30 08:50:49 +13:00
}
/////////////////////////////
// Screen Events
/////////////////////////////
protected [EVENT.SCREEN_UPDATED]({ width, height, rate }: message.ScreenSize) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.SCREEN_UPDATED')
2020-12-30 08:50:49 +13:00
Vue.set(this.state.screen, 'size', { width, height, rate })
2021-02-01 02:25:17 +13:00
this.emit('room.screen.updated', width, height, rate)
2020-12-30 08:50:49 +13:00
}
2021-02-12 07:03:31 +13:00
2020-12-30 08:50:49 +13:00
/////////////////////////////
// Clipboard Events
/////////////////////////////
protected [EVENT.CLIPBOARD_UPDATED]({ text }: message.ClipboardData) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.CLIPBOARD_UPDATED')
2020-12-30 08:50:49 +13:00
Vue.set(this.state.control, 'clipboard', { text })
2021-02-01 02:25:17 +13:00
this.emit('room.clipboard.updated', text)
2020-12-30 08:50:49 +13:00
}
/////////////////////////////
// Broadcast Events
/////////////////////////////
protected [EVENT.BORADCAST_STATUS]({ event, url, is_active }: message.BroadcastStatus) {
2021-01-16 05:17:49 +13:00
this._log.debug('EVENT.BORADCAST_STATUS')
2020-12-30 08:50:49 +13:00
// TODO: Handle.
2021-02-01 02:25:17 +13:00
this.emit('room.broadcast.status', is_active, url)
2020-12-30 08:50:49 +13:00
}
2021-01-21 11:59:32 +13:00
2021-01-29 04:08:17 +13:00
/////////////////////////////
// Send Events
/////////////////////////////
protected [EVENT.SEND_UNICAST]({ sender, subject, body }: message.SendMessage) {
this._log.debug('EVENT.SEND_UNICAST')
this.emit('receive.unicast', sender, subject, body)
}
protected [EVENT.SEND_BROADCAST]({ sender, subject, body }: message.SendMessage) {
this._log.debug('EVENT.BORADCAST_STATUS')
this.emit('receive.broadcast', sender, subject, body)
}
2021-01-21 11:59:32 +13:00
/////////////////////////////
// FileChooserDialog Events
/////////////////////////////
2021-03-14 12:08:52 +13:00
protected [EVENT.FILE_CHOOSER_DIALOG_OPENED]({ id }: message.SessionID) {
2021-01-21 11:59:32 +13:00
this._log.debug('EVENT.FILE_CHOOSER_DIALOG_OPENED')
2021-03-14 12:08:52 +13:00
if (id == this.state.session_id) {
2021-02-01 02:25:17 +13:00
this.emit('upload.dialog.requested')
2021-01-21 11:59:32 +13:00
} else {
2021-02-01 02:25:17 +13:00
this.emit('upload.dialog.overlay', id)
2021-01-21 11:59:32 +13:00
}
}
2021-03-14 12:08:52 +13:00
protected [EVENT.FILE_CHOOSER_DIALOG_CLOSED]({ id }: message.SessionID) {
2021-01-21 11:59:32 +13:00
this._log.debug('EVENT.FILE_CHOOSER_DIALOG_CLOSED')
2021-02-01 02:25:17 +13:00
this.emit('upload.dialog.closed')
2021-01-21 11:59:32 +13:00
}
2020-12-30 08:50:49 +13:00
}