uci-socket/examples/client2.mjs

38 lines
954 B
JavaScript
Raw Normal View History

2018-01-20 14:30:21 -08:00
import Consumer from '../src/consumer'
const USOCKET = __dirname + '/sample.sock'
class Client extends Consumer {
constructor(path,opts) {
super(path,opts)
}
async _packetProcess (packet) {
this[packet.cmd](packet)
}
async reply (packet) {
console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`)
console.log(`Socket replied with data: ${packet.data}`)
}
2018-01-20 14:30:21 -08:00
}
const client1= new Client({path:USOCKET,name:'example-consumer1' })
const client2 = new Client({path:USOCKET,name:'example-consumer2'})
2018-01-20 14:30:21 -08:00
let packet1 = {name: 'client1', cmd:'doit', data:'data sent by client1'}
let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
;
(async () => {
await Promise.all([client1.connect(),client2.connect()])
await Promise.all([client1.send(packet1),client2.send(packet2)])
client1.end()
client2.end()
2018-01-20 14:30:21 -08:00
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})