uci-base/src/packet.mjs

46 lines
1.5 KiB
JavaScript

export default {
Socket:{
_process: async function (packet) {
if (!packet.cmd) return {error: 'no command in packet', packet: packet }
if (this.context) if (this.context[packet.cmd]) return await this.context[packet.cmd].bind(this.context)(packet)
if (this[packet.cmd]) return await this[packet.cmd](packet)
return {error: 'no socket processing function supplied for command', packet: packet }
},
echo: packet => {
packet.processed = true
packet.cmd = 'log'
packet.info = 'default socket echo'
return packet
}
},
Consumer: {
_process: async function (packet) {
if (packet.error) return this.error(packet)
if (packet.cmd) {
if (this.context) if (this.context[packet.cmd]) return await this.context[packet.cmd].bind(this.context)(packet)
if (this[packet.cmd]) return await this[packet.cmd](packet)
packet = {error:'no consumer processing function supplied for command',packet:packet}
return this.error(packet)
} else {
packet = {error:'no command in packet',packet:packet}
return this.error(packet)
}
},
error: function (packet) {
console.log('==============Packet ERROR==========')
console.log(packet.error )
console.dir(packet.packet)
console.log('===========================')
},
log: packet => {
console.log('==============Packet returned from socket==========')
console.dir(packet)
console.log('===========================')
}
}
}