uci-socket/examples/client.mjs

31 lines
1.0 KiB
JavaScript

import Consumer from '../src/consumer'
const USOCKET = __dirname + '/sample.sock'
const client1= new Consumer(USOCKET, {log:false,name:'example-consumer1' })
const client2 = new Consumer(USOCKET, {log:false,name:'example-consumer2'})
let packet1 = {name: 'client1', cmd:'doit', data:'data sent by client1'}
let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
// This is your client handler object waiting on a message to do something
let app = {
processIt: function processPacket (packet) {
console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`)
},
pp: 'processIt'
}
Object.assign(client1,app)
// alternatively if you pass an object to the connect method it will merge whatever you pass
// or even better extend the consumer class and then make instances. See client2
;
(async () => {
await Promise.all([client1.connect(),client2.connect(app)])
await Promise.all([client1.send(packet1),client2.send(packet2)])
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})