74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
import Consumer from '../src/consumer'
|
|
import btc from 'better-try-catch'
|
|
|
|
// const client1= new Consumer({name:'example-consumer1' })
|
|
const client= new Consumer({path:true, name:'example-consumer', initTimeout:30 })
|
|
|
|
// This is your client handler object waiting on a message to do something
|
|
async function processor (packet) {
|
|
// console.log('packet being processed at socket', packet)
|
|
if (packet.cmd) {
|
|
if (this[packet.cmd]) return await this[packet.cmd](packet)
|
|
else {
|
|
console.log('no processing function for command', packet.cmd)
|
|
return {error: 'command has no processing function', packet: packet }
|
|
}
|
|
}
|
|
console.log('no command in packet', packet)
|
|
return {error: 'no command in packet', packet: packet }
|
|
}
|
|
|
|
|
|
client.registerPacketProcessor(processor)
|
|
|
|
client.reply = (packet) => {
|
|
return new Promise(resolve => {
|
|
console.log('generic reply command from server\n',packet.data)
|
|
resolve()
|
|
})
|
|
}
|
|
|
|
client.onconnect = (packet) => {
|
|
return new Promise(resolve => {
|
|
console.log('on connect server sent command processed here\n',packet.data)
|
|
resolve()
|
|
})
|
|
}
|
|
|
|
client.pushed = (packet) => {
|
|
return new Promise(resolve => {
|
|
console.log('pushed packet\n',packet.status)
|
|
resolve()
|
|
})
|
|
}
|
|
|
|
|
|
|
|
client.on('connection', event => {
|
|
console.log('============ connection update ============')
|
|
console.log(event.id)
|
|
console.log(event.msg)
|
|
console.log(`Consumer is ${event.connected ? 'connected' : 'disconnected'}`)
|
|
console.log(`Authenticated? ${event.authenticated || false}`)
|
|
console.log('======================================')
|
|
})
|
|
|
|
;
|
|
(async () => {
|
|
console.log('ready at start',client.ready)
|
|
// await Promise.all([client1.connect(),client2.connect()])
|
|
let [err, res] = await btc(client.connect)()
|
|
if (err) {
|
|
console.log('error', err)
|
|
} else {
|
|
console.log(res)
|
|
let packet = {name: 'client', cmd:'doit', data:'sent by client'}
|
|
console.log('=============sending a test packet=========\n', packet)
|
|
console.log('can also await relay of any send too for processing in sync manner\n',await client.send(packet))
|
|
console.log('===============')
|
|
}
|
|
|
|
})().catch(err => {
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
})
|