WebSocket types & messages refactored using new spec.

This commit is contained in:
Miroslav Šedivý 2020-11-30 22:00:07 +01:00
parent 34ac9098f0
commit 267ef5baa0
5 changed files with 143 additions and 259 deletions

View File

@ -93,9 +93,7 @@
inverse: true, inverse: true,
sensitivity: 1, sensitivity: 1,
}, },
clipboard: { clipboard: null,
data: null,
},
host: null, host: null,
}, },
screen: { screen: {
@ -227,8 +225,8 @@
} }
public setScreenSize(width: number, height: number, rate: number) { public setScreenSize(width: number, height: number, rate: number) {
this.api.admin.screenConfigurationChange({ screenConfigurationPayload: { width, height, rate } }) //this.api.admin.screenConfigurationChange({ screenConfigurationPayload: { width, height, rate } })
//this.websocket.send('screen/set', { width, height, rate }) this.websocket.send('screen/set', { width, height, rate })
} }
///////////////////////////// /////////////////////////////
@ -238,11 +236,6 @@
// component size change // component size change
this.observer.observe(this._component) this.observer.observe(this._component)
// host change
this.events.on('control.host', (id: string | null) => {
Vue.set(this.state.member, 'is_controlling', id != null && id === this.state.member.id)
})
// fullscreen change // fullscreen change
this._component.addEventListener('fullscreenchange', () => { this._component.addEventListener('fullscreenchange', () => {
Vue.set(this.state.video, 'fullscreen', document.fullscreenElement !== null) Vue.set(this.state.video, 'fullscreen', document.fullscreenElement !== null)
@ -256,8 +249,6 @@
this.websocket.on('message', async (event: string, payload: any) => { this.websocket.on('message', async (event: string, payload: any) => {
switch (event) { switch (event) {
case 'signal/provide': case 'signal/provide':
Vue.set(this.state.member, 'id', payload.id)
try { try {
let sdp = await this.webrtc.connect(payload.sdp, payload.lite, payload.ice) let sdp = await this.webrtc.connect(payload.sdp, payload.lite, payload.ice)
this.websocket.send('signal/answer', { sdp }) this.websocket.send('signal/answer', { sdp })
@ -267,15 +258,16 @@
}) })
this.websocket.on('connecting', () => { this.websocket.on('connecting', () => {
Vue.set(this.state.connection, 'websocket', 'connecting') Vue.set(this.state.connection, 'websocket', 'connecting')
this.events.emit('system.websocket', 'connecting') this.events.emit('internal.websocket', 'connecting')
}) })
this.websocket.on('connected', () => { this.websocket.on('connected', () => {
this.websocket.send('signal/request')
Vue.set(this.state.connection, 'websocket', 'connected') Vue.set(this.state.connection, 'websocket', 'connected')
this.events.emit('system.websocket', 'connected') this.events.emit('internal.websocket', 'connected')
}) })
this.websocket.on('disconnected', () => { this.websocket.on('disconnected', () => {
Vue.set(this.state.connection, 'websocket', 'disconnected') Vue.set(this.state.connection, 'websocket', 'disconnected')
this.events.emit('system.websocket', 'disconnected') this.events.emit('internal.websocket', 'disconnected')
this.webrtc.disconnect() this.webrtc.disconnect()
// TODO: reset state // TODO: reset state
@ -308,15 +300,15 @@
}) })
this.webrtc.on('connecting', () => { this.webrtc.on('connecting', () => {
Vue.set(this.state.connection, 'webrtc', 'connecting') Vue.set(this.state.connection, 'webrtc', 'connecting')
this.events.emit('system.webrtc', 'connecting') this.events.emit('internal.webrtc', 'connecting')
}) })
this.webrtc.on('connected', () => { this.webrtc.on('connected', () => {
Vue.set(this.state.connection, 'webrtc', 'connected') Vue.set(this.state.connection, 'webrtc', 'connected')
this.events.emit('system.webrtc', 'connected') this.events.emit('internal.webrtc', 'connected')
}) })
this.webrtc.on('disconnected', () => { this.webrtc.on('disconnected', () => {
Vue.set(this.state.connection, 'webrtc', 'disconnected') Vue.set(this.state.connection, 'webrtc', 'disconnected')
this.events.emit('system.webrtc', 'disconnected') this.events.emit('internal.webrtc', 'disconnected')
// @ts-ignore // @ts-ignore
this._video.src = null this._video.src = null
}) })

View File

@ -1,72 +1,30 @@
export const EVENT = { export const SYSTEM_INIT = 'system/init'
SYSTEM: { export const SYSTEM_ADMIN = 'system/admin'
DISCONNECT: 'system/disconnect', export const SYSTEM_DISCONNECT = 'system/disconnect'
},
SIGNAL: {
ANSWER: 'signal/answer',
PROVIDE: 'signal/provide',
},
MEMBER: {
LIST: 'member/list',
CONNECTED: 'member/connected',
DISCONNECTED: 'member/disconnected',
},
CONTROL: {
LOCKED: 'control/locked',
RELEASE: 'control/release',
REQUEST: 'control/request',
REQUESTING: 'control/requesting',
GIVE: 'control/give',
CLIPBOARD: 'control/clipboard',
KEYBOARD: 'control/keyboard',
},
SCREEN: {
CONFIGURATIONS: 'screen/configurations',
RESOLUTION: 'screen/resolution',
SET: 'screen/set',
},
BROADCAST: {
STATUS: 'broadcast/status',
CREATE: 'broadcast/create',
DESTROY: 'broadcast/destroy',
},
ADMIN: {
CONTROL: 'admin/control',
RELEASE: 'admin/release',
GIVE: 'admin/give',
},
} as const
export type Events = typeof EVENT export const SIGNAL_REQUEST = 'signal/request'
export const SIGNAL_ANSWER = 'signal/answer'
export const SIGNAL_PROVIDE = 'signal/provide'
export type WebSocketEvents = export const MEMBER_CONNECTED = 'member/connected'
| SystemEvents export const MEMBER_UPDATED = 'member/updated' // TODO: New.
| SignalEvents export const MEMBER_DISCONNECTED = 'member/disconnected'
| MemberEvents
| ControlEvents
| ScreenEvents
| BroadcastEvents
| AdminEvents
export type SystemEvents = typeof EVENT.SYSTEM.DISCONNECT export const CONTROL_HOST = 'control/host'
export const CONTROL_RELEASE = 'control/release'
export const CONTROL_REQUEST = 'control/request'
export const CONTROL_MOVE = 'control/move' // TODO: New. (fallback)
export const CONTROL_SCROLL = 'control/scroll' // TODO: New. (fallback)
export const CONTROL_KEYDOWN = 'control/keydown' // TODO: New. (fallback)
export const CONTROL_KEYUP = 'control/keyup' // TODO: New. (fallback)
export type SignalEvents = typeof EVENT.SIGNAL.ANSWER | typeof EVENT.SIGNAL.PROVIDE export const SCREEN_UPDATED = 'screen/updated'
export const SCREEN_SET = 'screen/set'
export type MemberEvents = typeof EVENT.MEMBER.LIST | typeof EVENT.MEMBER.CONNECTED | typeof EVENT.MEMBER.DISCONNECTED export const CLIPBOARD_UPDATED = 'clipboard/updated'
export const CLIPBOARD_SET = 'clipboard/set'
export type ControlEvents = export const KEYBOARD_MODIFIERS = 'keyboard/modifiers'
| typeof EVENT.CONTROL.LOCKED export const KEYBOARD_LAYOUT = 'keyboard/layout'
| typeof EVENT.CONTROL.RELEASE
| typeof EVENT.CONTROL.REQUEST
| typeof EVENT.CONTROL.GIVE
| typeof EVENT.CONTROL.CLIPBOARD
| typeof EVENT.CONTROL.KEYBOARD
export type ScreenEvents = typeof EVENT.SCREEN.CONFIGURATIONS | typeof EVENT.SCREEN.RESOLUTION | typeof EVENT.SCREEN.SET export const BORADCAST_STATUS = 'broadcast/status'
export type BroadcastEvents =
| typeof EVENT.BROADCAST.STATUS
| typeof EVENT.BROADCAST.CREATE
| typeof EVENT.BROADCAST.DESTROY
export type AdminEvents = typeof EVENT.ADMIN.CONTROL | typeof EVENT.ADMIN.RELEASE | typeof EVENT.ADMIN.GIVE

View File

@ -1,178 +1,137 @@
import { export interface Message {
EVENT, event: string | undefined
WebSocketEvents, payload: any
SystemEvents,
SignalEvents,
MemberEvents,
ControlEvents,
ScreenEvents,
BroadcastEvents,
AdminEvents,
} from './events'
import { Member, ScreenConfigurations, ScreenResolution } from './structs'
export type WebSocketMessages =
| WebSocketMessage
| SignalProvideMessage
| SignalAnswerMessage
| MemberListMessage
| MemberConnectMessage
| MemberDisconnectMessage
| ControlMessage
| ScreenResolutionMessage
| ScreenConfigurationsMessage
export type WebSocketPayloads =
| SignalProvidePayload
| SignalAnswerPayload
| MemberListPayload
| Member
| ControlPayload
| ControlClipboardPayload
| ControlKeyboardPayload
| ScreenResolutionPayload
| ScreenConfigurationsPayload
| AdminPayload
| BroadcastStatusPayload
| BroadcastCreatePayload
export interface WebSocketMessage {
event: WebSocketEvents | string
} }
/* /////////////////////////////
SYSTEM MESSAGES/PAYLOADS // System
*/ /////////////////////////////
// system/disconnect
export interface DisconnectMessage extends WebSocketMessage, DisconnectPayload { export interface SystemInit {
event: typeof EVENT.SYSTEM.DISCONNECT event: string | undefined
control_host: ControlHost
screen_size: ScreenSize
members: MemberData[]
} }
export interface DisconnectPayload {
export interface SystemAdmin {
event: string | undefined
screen_sizes_list: ScreenSize[]
broadcast_status: BroadcastStatus
}
// TODO: New.
export interface SystemDisconnect {
event: string | undefined
message: string message: string
} }
/* /////////////////////////////
SIGNAL MESSAGES/PAYLOADS // Signal
*/ /////////////////////////////
// signal/provide
export interface SignalProvideMessage extends WebSocketMessage, SignalProvidePayload { export interface SignalProvide {
event: typeof EVENT.SIGNAL.PROVIDE event: string | undefined
} sdp: string
export interface SignalProvidePayload {
id: string
lite: boolean lite: boolean
ice: string[] ice: string[]
}
export interface SignalAnswer {
event: string | undefined
sdp: string sdp: string
} }
// signal/answer /////////////////////////////
export interface SignalAnswerMessage extends WebSocketMessage, SignalAnswerPayload { // Member
event: typeof EVENT.SIGNAL.ANSWER /////////////////////////////
}
export interface SignalAnswerPayload {
sdp: string
}
/* // TODO: New.
MEMBER MESSAGES/PAYLOADS export interface MemberID {
*/ event: string | undefined
// member/list
export interface MemberListMessage extends WebSocketMessage, MemberListPayload {
event: typeof EVENT.MEMBER.LIST
}
export interface MemberListPayload {
members: Member[]
}
// member/connected
export interface MemberConnectMessage extends WebSocketMessage, MemberPayload {
event: typeof EVENT.MEMBER.CONNECTED
}
export type MemberPayload = Member
// member/disconnected
export interface MemberDisconnectMessage extends WebSocketMessage, MemberPayload {
event: typeof EVENT.MEMBER.DISCONNECTED
}
export interface MemberDisconnectPayload {
id: string id: string
} }
/* // TODO: New.
CONTROL MESSAGES/PAYLOADS export interface MemberData {
*/ event: string | undefined
// control/locked & control/release & control/request
export interface ControlMessage extends WebSocketMessage, ControlPayload {
event: ControlEvents
}
export interface ControlPayload {
id: string id: string
name: string
is_admin: boolean
} }
export interface ControlTargetPayload { /////////////////////////////
id: string // Control
target: string /////////////////////////////
export interface ControlHost {
event: string | undefined
has_host: boolean
host_id: string | undefined
} }
export interface ControlClipboardPayload { // TODO: New.
export interface ControlMove {
event: string | undefined
x: number
y: number
}
// TODO: New.
export interface ControlScroll {
event: string | undefined
x: number
y: number
}
// TODO: New.
export interface ControlKey {
event: string | undefined
key: number
}
/////////////////////////////
// Screen
/////////////////////////////
export interface ScreenSize {
event: string | undefined
width: number
height: number
rate: number
}
/////////////////////////////
// Clipboard
/////////////////////////////
export interface ClipboardData {
event: string | undefined
text: string text: string
} }
export interface ControlKeyboardPayload { /////////////////////////////
layout?: string // Keyboard
capsLock?: boolean /////////////////////////////
numLock?: boolean
scrollLock?: boolean export interface KeyboardModifiers {
event: string | undefined
caps_lock: boolean
num_lock: boolean
scroll_lock: boolean
} }
/* export interface KeyboardLayout {
SCREEN PAYLOADS event: string | undefined
*/ layout: string
export interface ScreenResolutionMessage extends WebSocketMessage, ScreenResolutionPayload {
event: ScreenEvents
} }
export interface ScreenResolutionPayload extends ScreenResolution { /////////////////////////////
id?: string // Broadcast
} /////////////////////////////
export interface ScreenConfigurationsMessage extends WebSocketMessage, ScreenConfigurationsPayload { export interface BroadcastStatus {
event: ScreenEvents event: string | undefined
} is_active: boolean
url: string | undefined
export interface ScreenConfigurationsPayload {
configurations: ScreenConfigurations
}
/*
BROADCAST PAYLOADS
*/
export interface BroadcastCreatePayload {
url: string
}
export interface BroadcastStatusPayload {
url: string
isActive: boolean
}
/*
ADMIN PAYLOADS
*/
export interface AdminMessage extends WebSocketMessage, AdminPayload {
event: AdminEvents
}
export interface AdminPayload {
id: string
}
export interface AdminTargetMessage extends WebSocketMessage, AdminTargetPayload {
event: AdminEvents
}
export interface AdminTargetPayload {
id: string
target?: string
} }

View File

@ -35,7 +35,7 @@ export interface Video {
///////////////////////////// /////////////////////////////
export interface Control { export interface Control {
scroll: Scroll scroll: Scroll
clipboard: Clipboard clipboard: Clipboard | null
host: Member | null host: Member | null
} }
@ -45,7 +45,7 @@ export interface Scroll {
} }
export interface Clipboard { export interface Clipboard {
data: string | null text: string
} }
///////////////////////////// /////////////////////////////

View File

@ -1,25 +0,0 @@
export interface Member {
id: string
name: string
admin: boolean
connected?: boolean
ignored?: boolean
}
export interface ScreenConfigurations {
[index: string]: ScreenConfiguration
}
export interface ScreenConfiguration {
width: number
height: number
rates: {
[index: string]: number
}
}
export interface ScreenResolution {
width: number
height: number
rate: number
}