uci-websocket/examples/server.js

31 lines
879 B
JavaScript
Raw Normal View History

import Socket from '../src'
async function packetProcess(packet) {
// 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
return new Promise(resolve => {
let res = {}
switch (packet.cmd) {
case 'echo':
res.msg = 'Echoing Back any payload propery'
res.payload = packet.payload
this.push(res)
break
default:
res.msg = `.cmd, command ${packet.cmd} was unknown at server`
res.sent_packet = packet
}
resolve(res)
})
}
// let test = new Test()
let test = new Socket({ port: 8090, clientTracking: true })
test.registerPacketProcessor(packetProcess)
;(async () => {
console.log(await test.create())
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n', err)
})