2018-01-08 13:06:01 -08:00
|
|
|
import Consumer from '../src/consumer'
|
|
|
|
|
2018-01-10 15:03:32 -08:00
|
|
|
const USOCKET = __dirname + '/sample.sock'
|
2018-01-08 13:06:01 -08:00
|
|
|
|
2018-01-20 14:30:21 -08:00
|
|
|
const client1= new Consumer(USOCKET, {log:false,name:'example-consumer1' })
|
|
|
|
const client2 = new Consumer(USOCKET, {log:false,name:'example-consumer2'})
|
2018-01-08 13:06:01 -08:00
|
|
|
|
2018-01-13 20:46:14 -08:00
|
|
|
let packet1 = {name: 'client1', cmd:'doit', data:'data sent by client1'}
|
|
|
|
let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
|
2018-01-08 13:06:01 -08:00
|
|
|
|
2018-01-13 20:46:14 -08:00
|
|
|
// This is your client handler object waiting on a message to do something
|
2018-01-10 15:03:32 -08:00
|
|
|
let app = {
|
|
|
|
processIt: function processPacket (packet) {
|
2018-01-20 14:30:21 -08:00
|
|
|
console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`)
|
2018-01-10 15:03:32 -08:00
|
|
|
},
|
2018-01-20 14:30:21 -08:00
|
|
|
pp: 'processIt'
|
2018-01-10 15:03:32 -08:00
|
|
|
}
|
|
|
|
|
2018-01-20 14:30:21 -08:00
|
|
|
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
|
2018-01-08 13:06:01 -08:00
|
|
|
;
|
|
|
|
(async () => {
|
|
|
|
|
2018-01-20 14:30:21 -08:00
|
|
|
await Promise.all([client1.connect(),client2.connect(app)])
|
2018-01-13 20:46:14 -08:00
|
|
|
await Promise.all([client1.send(packet1),client2.send(packet2)])
|
2018-01-08 13:06:01 -08:00
|
|
|
|
|
|
|
})().catch(err => {
|
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
|
|
})
|