37 lines
1018 B
JavaScript
37 lines
1018 B
JavaScript
import Socket from '@uci/websocket'
|
|
|
|
// ws server to example ws client
|
|
|
|
async function packetProcess (packet) {
|
|
return new Promise(resolve => {
|
|
let res = {}
|
|
switch (packet.cmd) {
|
|
case 'echo':
|
|
res.msg = 'Echoing Back any payload propery'
|
|
res.payload = packet.payload
|
|
this.push({msg:'pushing echo to to any clients also', res:res})
|
|
break
|
|
case 'on':
|
|
case 'off':
|
|
res.msg = `Command turn ${packet.cmd} was sent for light id: ${packet.id ||'none!'} of brightness: ${packet.brightness || 0}`
|
|
res.payload = {switch:packet.cmd}
|
|
this.push(res)
|
|
break
|
|
default:
|
|
res.msg = `command ${packet.cmd} was unknown at server echo payload back`
|
|
res.payload = {}
|
|
}
|
|
|
|
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)
|
|
})
|