2020-11-06 05:15:21 +13:00
|
|
|
import EventEmitter from 'eventemitter3'
|
|
|
|
import { Logger } from '../utils/logger'
|
|
|
|
|
|
|
|
export const timeout = 15000
|
|
|
|
|
|
|
|
export interface NekoWebSocketEvents {
|
|
|
|
connecting: () => void
|
|
|
|
connected: () => void
|
|
|
|
disconnected: (error?: Error) => void
|
2020-11-06 06:06:55 +13:00
|
|
|
message: (event: string, payload: any) => void
|
2020-11-06 05:15:21 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|
|
|
private _ws?: WebSocket
|
|
|
|
private _timeout?: NodeJS.Timeout
|
|
|
|
private _log: Logger
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super()
|
2020-11-06 05:20:37 +13:00
|
|
|
|
2020-11-06 05:15:21 +13:00
|
|
|
this._log = new Logger('websocket')
|
|
|
|
}
|
|
|
|
|
2021-01-30 08:13:37 +13:00
|
|
|
get supported() {
|
|
|
|
return typeof WebSocket !== 'undefined' && WebSocket.OPEN === 1
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:15:21 +13:00
|
|
|
get connected() {
|
|
|
|
return typeof this._ws !== 'undefined' && this._ws.readyState === WebSocket.OPEN
|
|
|
|
}
|
|
|
|
|
2021-01-30 11:14:59 +13:00
|
|
|
public connect(url: string) {
|
2020-11-06 05:15:21 +13:00
|
|
|
if (this.connected) {
|
|
|
|
throw new Error('attempting to create websocket while connection open')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('connecting')
|
|
|
|
|
2021-01-30 11:14:59 +13:00
|
|
|
this._ws = new WebSocket(url)
|
2021-01-16 05:17:49 +13:00
|
|
|
this._log.info(`connecting`)
|
2020-11-06 05:15:21 +13:00
|
|
|
|
|
|
|
this._ws.onopen = this.onConnected.bind(this)
|
|
|
|
this._ws.onclose = this.onDisconnected.bind(this, new Error('websocket closed'))
|
2020-11-06 06:06:55 +13:00
|
|
|
this._ws.onerror = this.onDisconnected.bind(this, new Error('websocket error'))
|
2020-11-06 05:15:21 +13:00
|
|
|
this._ws.onmessage = this.onMessage.bind(this)
|
|
|
|
|
|
|
|
this._timeout = setTimeout(this.onTimeout.bind(this), timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
public disconnect() {
|
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.connected) {
|
|
|
|
try {
|
|
|
|
this._ws!.close()
|
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
this._ws = undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public send(event: string, payload?: any) {
|
|
|
|
if (!this.connected) {
|
|
|
|
this._log.warn(`attempting to send message while disconnected`)
|
|
|
|
return
|
|
|
|
}
|
2020-11-06 05:20:37 +13:00
|
|
|
|
2020-11-06 05:15:21 +13:00
|
|
|
this._log.debug(`sending event '${event}' ${payload ? `with payload: ` : ''}`, payload)
|
|
|
|
this._ws!.send(JSON.stringify({ event, ...payload }))
|
|
|
|
}
|
|
|
|
|
|
|
|
private onMessage(e: MessageEvent) {
|
|
|
|
const { event, ...payload } = JSON.parse(e.data)
|
|
|
|
|
2020-11-06 06:06:55 +13:00
|
|
|
this._log.debug(`received websocket event ${event} ${payload ? `with payload: ` : ''}`, payload)
|
|
|
|
this.emit('message', event, payload)
|
2020-11-06 05:15:21 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
private onConnected() {
|
|
|
|
if (this._timeout) {
|
|
|
|
clearTimeout(this._timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.connected) {
|
|
|
|
this._log.warn(`onConnected called while being disconnected`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-16 05:17:49 +13:00
|
|
|
this._log.info(`connected`)
|
2020-11-06 05:15:21 +13:00
|
|
|
this.emit('connected')
|
|
|
|
}
|
|
|
|
|
|
|
|
private onTimeout() {
|
2021-01-16 05:17:49 +13:00
|
|
|
this._log.info(`connection timeout`)
|
2020-11-06 05:15:21 +13:00
|
|
|
this.onDisconnected(new Error('connection timeout'))
|
|
|
|
}
|
|
|
|
|
|
|
|
private onDisconnected(reason?: Error) {
|
2021-01-16 05:17:49 +13:00
|
|
|
this._log.info(`disconnected:`, reason?.message)
|
2020-11-06 05:20:37 +13:00
|
|
|
|
2020-11-06 05:15:21 +13:00
|
|
|
this.disconnect()
|
|
|
|
this.emit('disconnected', reason)
|
|
|
|
}
|
|
|
|
}
|