2019-01-01 19:11:16 -08:00
|
|
|
import Socket from '../src'
|
2018-04-05 15:05:59 -07:00
|
|
|
|
2019-01-01 19:11:16 -08:00
|
|
|
async function packetProcess(packet) {
|
2019-08-13 09:06:07 -07:00
|
|
|
// Here one registers a packet processor which gets an plain object hash from the client
|
|
|
|
// should return a promise
|
|
|
|
// for example if client sends a property cmd on can take action on that
|
2018-05-24 12:23:49 -07:00
|
|
|
return new Promise(resolve => {
|
|
|
|
let res = {}
|
2019-08-13 09:06:07 -07:00
|
|
|
switch (packet.cmd) {
|
|
|
|
case 'echo':
|
|
|
|
res.msg = 'Echoing Back any payload propery'
|
2019-01-01 19:11:16 -08:00
|
|
|
res.payload = packet.payload
|
2019-08-13 09:06:07 -07:00
|
|
|
this.push(res)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
res.msg = `.cmd, command ${packet.cmd} was unknown at server`
|
|
|
|
res.sent_packet = packet
|
2019-01-01 19:11:16 -08:00
|
|
|
}
|
|
|
|
resolve(res)
|
2018-05-24 12:23:49 -07:00
|
|
|
})
|
|
|
|
}
|
2018-04-05 15:05:59 -07:00
|
|
|
|
2018-05-24 12:23:49 -07:00
|
|
|
// let test = new Test()
|
2019-01-01 19:11:16 -08:00
|
|
|
let test = new Socket({ port: 8090, clientTracking: true })
|
2018-05-24 12:23:49 -07:00
|
|
|
test.registerPacketProcessor(packetProcess)
|
2019-01-01 19:11:16 -08:00
|
|
|
;(async () => {
|
2018-04-05 15:05:59 -07:00
|
|
|
console.log(await test.create())
|
|
|
|
})().catch(err => {
|
2019-01-01 19:11:16 -08:00
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n', err)
|
2018-04-05 15:05:59 -07:00
|
|
|
})
|