2018-04-05 15:05:59 -07:00
|
|
|
import WebSocket from 'ws'
|
|
|
|
import btc from 'better-try-catch'
|
|
|
|
import _ON_DEATH from 'death' //this is intentionally ugly
|
|
|
|
import clone from 'clone'
|
|
|
|
|
2019-02-14 14:01:17 -08:00
|
|
|
import logger from '@uci-utils/logger'
|
2018-04-05 15:05:59 -07:00
|
|
|
let log = {}
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
/**
|
|
|
|
* Socket - a websocket that supports uci packets
|
|
|
|
* @class WebSocket
|
|
|
|
* @extends ws.server
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Socket extends WebSocket.Server {
|
|
|
|
constructor(opts = {}) {
|
2018-04-05 15:05:59 -07:00
|
|
|
opts.host = opts.host || '0.0.0.0'
|
2018-05-24 12:23:49 -07:00
|
|
|
opts.port = opts.port || 8090
|
|
|
|
opts.clientTracking = opts.clientTracking || true
|
2018-04-05 15:05:59 -07:00
|
|
|
super(opts)
|
2019-01-01 19:11:16 -08:00
|
|
|
this.id = opts.id || opts.name || 'Websocket:' + new Date().getTime()
|
|
|
|
this.opts = opts // for use to recover from selected errors
|
2018-04-05 15:05:59 -07:00
|
|
|
//self bindings
|
|
|
|
this._listen = this._listen.bind(this)
|
|
|
|
this.create = this.create.bind(this)
|
2019-01-01 19:11:16 -08:00
|
|
|
log = logger({
|
|
|
|
file: 'src/socket.js',
|
|
|
|
class: 'Socket',
|
|
|
|
name: 'websocket',
|
|
|
|
id: this.id
|
|
|
|
})
|
2018-04-05 15:05:59 -07:00
|
|
|
} // end constructor
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
async create() {
|
2018-04-05 15:05:59 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-01 19:11:16 -08:00
|
|
|
_ON_DEATH(async () => {
|
2018-04-05 15:05:59 -07:00
|
|
|
log.info('\nhe\'s dead jim')
|
|
|
|
await this._destroy()
|
|
|
|
})
|
|
|
|
process.once('SIGUSR2', async () => {
|
|
|
|
await this._destroy
|
|
|
|
process.kill(process.pid, 'SIGUSR2')
|
|
|
|
})
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
this.on('error', async err => {
|
2018-04-05 15:05:59 -07:00
|
|
|
log.fatal(err, 'socket server error')
|
|
|
|
console.error(err, 'socket server error')
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.on('listening', async () => {
|
|
|
|
this._listen()
|
|
|
|
log.info('websocket server created and listening at', this.address())
|
2019-01-01 19:11:16 -08:00
|
|
|
resolve(
|
|
|
|
`websocket ready and listening at ${this.address().address}:${
|
|
|
|
this.address().port
|
|
|
|
}`
|
|
|
|
)
|
2018-04-05 15:05:59 -07:00
|
|
|
})
|
|
|
|
})
|
|
|
|
} // end create
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
registerPacketProcessor(func) {
|
2018-04-05 15:05:59 -07:00
|
|
|
this._packetProcess = func
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
_listen() {
|
2018-05-24 12:23:49 -07:00
|
|
|
this.on('connection', async (socket, req) => {
|
|
|
|
let send = this._send.bind(socket)
|
2019-01-01 19:11:16 -08:00
|
|
|
log.info({ req: req }, 'new consumer connecting')
|
2018-05-24 12:23:49 -07:00
|
|
|
socket.address = req.remoteAddress
|
2019-01-01 19:11:16 -08:00
|
|
|
socket.on('message', messageProcess.bind(this, socket))
|
2018-04-05 15:05:59 -07:00
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
async function messageProcess(client, strPacket) {
|
|
|
|
log.info({ packet: strPacket }, ' incoming packet on web socket side')
|
2018-04-05 15:05:59 -07:00
|
|
|
let res = {}
|
|
|
|
let [err, packet] = btc(JSON.parse)(strPacket)
|
2018-05-24 12:23:49 -07:00
|
|
|
log.info('packet', err, packet)
|
2019-01-01 19:11:16 -08:00
|
|
|
if (err) {
|
|
|
|
res = { error: `Could not parse JSON: ${packet}` }
|
|
|
|
} else {
|
2018-05-24 12:23:49 -07:00
|
|
|
if (packet.clientID) {
|
|
|
|
client.ID = packet.clientID
|
2019-01-01 19:11:16 -08:00
|
|
|
res.cmd = 'ackID'
|
|
|
|
} else {
|
|
|
|
res = (await this._packetProcess(clone(packet))) || {}
|
|
|
|
if (Object.keys(res).length === 0)
|
|
|
|
res = {
|
|
|
|
error:
|
|
|
|
'socket packet command function likely did not return a promise',
|
|
|
|
packet: packet
|
|
|
|
}
|
2018-05-24 12:23:49 -07:00
|
|
|
}
|
2018-04-05 15:05:59 -07:00
|
|
|
}
|
2018-05-24 12:23:49 -07:00
|
|
|
if (packet) {
|
2019-01-01 19:11:16 -08:00
|
|
|
res._header = clone(packet._header, false) || {} //make sure return packet has header with id in case it was removed in processing
|
|
|
|
delete packet._header // remove before adding to response header as request
|
2018-05-24 12:23:49 -07:00
|
|
|
} else res._header = {}
|
2019-01-01 19:11:16 -08:00
|
|
|
res._header.request = clone(packet, false)
|
|
|
|
res._header.responder = { name: this.name, instanceID: this.id }
|
2018-04-05 15:05:59 -07:00
|
|
|
res._header.socket = this.address()
|
2019-01-01 19:11:16 -08:00
|
|
|
if (!res.cmd) res.cmd = 'reply' // by default return command is 'reply'
|
|
|
|
log.info({ packet: res }, await send(res))
|
2018-04-05 15:05:59 -07:00
|
|
|
}
|
|
|
|
}) // end connected consumer
|
|
|
|
log.info('socket created')
|
|
|
|
} // end listen
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
async _destroy() {
|
2018-04-05 15:05:59 -07:00
|
|
|
log.info('closing down socket')
|
|
|
|
await this.close()
|
|
|
|
log.info('all connections closed....exiting')
|
|
|
|
process.exit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// default packet process, just a simple echo - replace
|
2019-01-01 19:11:16 -08:00
|
|
|
async _packetProcess(packet) {
|
2018-04-05 15:05:59 -07:00
|
|
|
return new Promise(resolve => {
|
|
|
|
resolve(packet)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
async push(packet, id) {
|
|
|
|
packet._header = { id: id || 'pushed' }
|
|
|
|
this.clients.forEach(async client => {
|
2018-05-24 12:23:49 -07:00
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
2019-01-01 19:11:16 -08:00
|
|
|
if (!id || id === client.ID) await this._send.bind(client)(packet)
|
2018-05-24 12:23:49 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// must have a consumer socket instance bound to call this function
|
2019-01-01 19:11:16 -08:00
|
|
|
async _send(packet) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (this.readyState !== 1)
|
|
|
|
reject(`Connection not Ready, CODE:${this.readyState}`)
|
|
|
|
let [err, message] = btc(JSON.stringify)(packet)
|
2018-04-05 15:05:59 -07:00
|
|
|
if (err) reject(`Could not JSON stringify: ${packet}`)
|
|
|
|
this.send(message)
|
2018-05-24 12:23:49 -07:00
|
|
|
resolve('sent packet')
|
2018-04-05 15:05:59 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} // end class
|
2019-01-01 19:11:16 -08:00
|
|
|
|
|
|
|
export default Socket
|