logging changed.

This commit is contained in:
Miroslav Šedivý 2021-01-15 17:17:49 +01:00
parent b9c68107d2
commit ed197d5c76
4 changed files with 26 additions and 23 deletions

View File

@ -3,6 +3,7 @@ import * as EVENT from '../types/events'
import * as message from '../types/messages' import * as message from '../types/messages'
import EventEmitter from 'eventemitter3' import EventEmitter from 'eventemitter3'
import { Logger } from '../utils/logger'
import { NekoWebSocket } from './websocket' import { NekoWebSocket } from './websocket'
import NekoState from '../types/state' import NekoState from '../types/state'
@ -24,11 +25,13 @@ export interface NekoEvents {
} }
export class NekoMessages extends EventEmitter<NekoEvents> { export class NekoMessages extends EventEmitter<NekoEvents> {
state: NekoState private state: NekoState
private _log: Logger
constructor(websocket: NekoWebSocket, state: NekoState) { constructor(websocket: NekoWebSocket, state: NekoState) {
super() super()
this._log = new Logger('messages')
this.state = state this.state = state
websocket.on('message', async (event: string, payload: any) => { websocket.on('message', async (event: string, payload: any) => {
// @ts-ignore // @ts-ignore
@ -36,7 +39,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
// @ts-ignore // @ts-ignore
this[event](payload) this[event](payload)
} else { } else {
console.log(`unhandled websocket event '${event}':`, payload) this._log.warn(`unhandled websocket event '${event}':`, payload)
} }
}) })
} }
@ -46,7 +49,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.SYSTEM_INIT](conf: message.SystemInit) { protected [EVENT.SYSTEM_INIT](conf: message.SystemInit) {
console.log('EVENT.SYSTEM_INIT') this._log.debug('EVENT.SYSTEM_INIT')
Vue.set(this.state, 'member_id', conf.member_id) Vue.set(this.state, 'member_id', conf.member_id)
Vue.set(this.state.control, 'implicit_hosting', conf.implicit_hosting) Vue.set(this.state.control, 'implicit_hosting', conf.implicit_hosting)
@ -62,7 +65,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
} }
protected [EVENT.SYSTEM_ADMIN]({ screen_sizes_list, broadcast_status }: message.SystemAdmin) { protected [EVENT.SYSTEM_ADMIN]({ screen_sizes_list, broadcast_status }: message.SystemAdmin) {
console.log('EVENT.SYSTEM_ADMIN') this._log.debug('EVENT.SYSTEM_ADMIN')
const list = screen_sizes_list.sort((a, b) => { const list = screen_sizes_list.sort((a, b) => {
if (b.width === a.width && b.height == a.height) { if (b.width === a.width && b.height == a.height) {
@ -79,13 +82,13 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
} }
protected [EVENT.SYSTEM_DISCONNECT]({ message }: message.SystemDisconnect) { protected [EVENT.SYSTEM_DISCONNECT]({ message }: message.SystemDisconnect) {
console.log('EVENT.SYSTEM_DISCONNECT') this._log.debug('EVENT.SYSTEM_DISCONNECT')
this.emit('system.disconnect', message) this.emit('system.disconnect', message)
// TODO: Handle. // TODO: Handle.
} }
protected [EVENT.CURSOR_IMAGE]({ uri, width, height, x, y }: message.CursorImage) { protected [EVENT.CURSOR_IMAGE]({ uri, width, height, x, y }: message.CursorImage) {
console.log('EVENT.CURSOR_IMAGE') this._log.debug('EVENT.CURSOR_IMAGE')
Vue.set(this.state.control, 'cursor', { uri, width, height, x, y }) Vue.set(this.state.control, 'cursor', { uri, width, height, x, y })
} }
@ -94,7 +97,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.SIGNAL_PROVIDE]({ event }: message.SignalProvide) { protected [EVENT.SIGNAL_PROVIDE]({ event }: message.SignalProvide) {
console.log('EVENT.SIGNAL_PROVIDE') this._log.debug('EVENT.SIGNAL_PROVIDE')
// TODO: Handle. // TODO: Handle.
} }
@ -103,25 +106,25 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.MEMBER_CREATED]({ id, ...data }: message.MemberData) { protected [EVENT.MEMBER_CREATED]({ id, ...data }: message.MemberData) {
console.log('EVENT.MEMBER_CREATED', id) this._log.debug('EVENT.MEMBER_CREATED', id)
Vue.set(this.state.members, id, data) Vue.set(this.state.members, id, data)
this.emit('member.created', id) this.emit('member.created', id)
} }
protected [EVENT.MEMBER_DELETED]({ id }: message.MemberID) { protected [EVENT.MEMBER_DELETED]({ id }: message.MemberID) {
console.log('EVENT.MEMBER_DELETED', id) this._log.debug('EVENT.MEMBER_DELETED', id)
Vue.delete(this.state.members, id) Vue.delete(this.state.members, id)
this.emit('member.deleted', id) this.emit('member.deleted', id)
} }
protected [EVENT.MEMBER_PROFILE]({ id, ...profile }: message.MemberProfile) { protected [EVENT.MEMBER_PROFILE]({ id, ...profile }: message.MemberProfile) {
console.log('EVENT.MEMBER_PROFILE', id) this._log.debug('EVENT.MEMBER_PROFILE', id)
Vue.set(this.state.members[id], 'profile', profile) Vue.set(this.state.members[id], 'profile', profile)
this.emit('member.profile', id) this.emit('member.profile', id)
} }
protected [EVENT.MEMBER_STATE]({ id, ...state }: message.MemberState) { protected [EVENT.MEMBER_STATE]({ id, ...state }: message.MemberState) {
console.log('EVENT.MEMBER_STATE', id) this._log.debug('EVENT.MEMBER_STATE', id)
Vue.set(this.state.members[id], 'state', state) Vue.set(this.state.members[id], 'state', state)
this.emit('member.state', id) this.emit('member.state', id)
} }
@ -131,7 +134,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.CONTROL_HOST]({ has_host, host_id }: message.ControlHost) { protected [EVENT.CONTROL_HOST]({ has_host, host_id }: message.ControlHost) {
console.log('EVENT.CONTROL_HOST') this._log.debug('EVENT.CONTROL_HOST')
if (has_host && host_id) { if (has_host && host_id) {
Vue.set(this.state.control, 'host_id', host_id) Vue.set(this.state.control, 'host_id', host_id)
@ -147,7 +150,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.SCREEN_UPDATED]({ width, height, rate }: message.ScreenSize) { protected [EVENT.SCREEN_UPDATED]({ width, height, rate }: message.ScreenSize) {
console.log('EVENT.SCREEN_UPDATED') this._log.debug('EVENT.SCREEN_UPDATED')
Vue.set(this.state.screen, 'size', { width, height, rate }) Vue.set(this.state.screen, 'size', { width, height, rate })
this.emit('screen.updated', width, height, rate) this.emit('screen.updated', width, height, rate)
} }
@ -156,7 +159,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.CLIPBOARD_UPDATED]({ text }: message.ClipboardData) { protected [EVENT.CLIPBOARD_UPDATED]({ text }: message.ClipboardData) {
console.log('EVENT.CLIPBOARD_UPDATED') this._log.debug('EVENT.CLIPBOARD_UPDATED')
Vue.set(this.state.control, 'clipboard', { text }) Vue.set(this.state.control, 'clipboard', { text })
this.emit('clipboard.updated', text) this.emit('clipboard.updated', text)
} }
@ -166,7 +169,7 @@ export class NekoMessages extends EventEmitter<NekoEvents> {
///////////////////////////// /////////////////////////////
protected [EVENT.BORADCAST_STATUS]({ event, url, is_active }: message.BroadcastStatus) { protected [EVENT.BORADCAST_STATUS]({ event, url, is_active }: message.BroadcastStatus) {
console.log('EVENT.BORADCAST_STATUS') this._log.debug('EVENT.BORADCAST_STATUS')
// TODO: Handle. // TODO: Handle.
this.emit('broadcast.status', is_active, url) this.emit('broadcast.status', is_active, url)
} }

View File

@ -41,7 +41,7 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
} }
public async connect(sdp: string, lite: boolean, servers: string[]): Promise<string> { public async connect(sdp: string, lite: boolean, servers: string[]): Promise<string> {
this._log.debug(`creating peer`) this._log.info(`connecting`)
if (!this.supported) { if (!this.supported) {
throw new Error('browser does not support webrtc') throw new Error('browser does not support webrtc')
@ -194,14 +194,14 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
return return
} }
this._log.debug(`connected`) this._log.info(`connected`)
this.emit('connected') this.emit('connected')
} }
private onDisconnected(reason?: Error) { private onDisconnected(reason?: Error) {
this.disconnect() this.disconnect()
this._log.debug(`disconnected:`, reason?.message) this._log.info(`disconnected:`, reason?.message)
this.emit('disconnected', reason) this.emit('disconnected', reason)
} }
} }

View File

@ -33,7 +33,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
this.emit('connecting') this.emit('connecting')
this._ws = new WebSocket(`${url}/ws?id=${encodeURIComponent(id)}&secret=${encodeURIComponent(secret)}`) this._ws = new WebSocket(`${url}/ws?id=${encodeURIComponent(id)}&secret=${encodeURIComponent(secret)}`)
this._log.debug(`connecting to ${this._ws.url}`) this._log.info(`connecting`)
this._ws.onopen = this.onConnected.bind(this) this._ws.onopen = this.onConnected.bind(this)
this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed')) this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed'))
@ -84,17 +84,17 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
return return
} }
this._log.debug(`connected`) this._log.info(`connected`)
this.emit('connected') this.emit('connected')
} }
private onTimeout() { private onTimeout() {
this._log.debug(`connection timeout`) this._log.info(`connection timeout`)
this.onDisconnected(new Error('connection timeout')) this.onDisconnected(new Error('connection timeout'))
} }
private onDisconnected(reason?: Error) { private onDisconnected(reason?: Error) {
this._log.debug(`disconnected:`, reason?.message) this._log.info(`disconnected:`, reason?.message)
this.disconnect() this.disconnect()
this.emit('disconnected', reason) this.emit('disconnected', reason)

View File

@ -20,6 +20,6 @@ export class Logger {
} }
public debug(...log: any[]) { public debug(...log: any[]) {
console.log('[%cNEKO%c] [' + this._scope + '] %cDBG', 'color: #498ad8;', '', 'color: #eae364;', ...log) console.debug('[%cNEKO%c] [' + this._scope + '] %cDBG', 'color: #498ad8;', '', 'color: #eae364;', ...log)
} }
} }