2018-01-08 13:06:01 -08:00
|
|
|
import Consumer from '../src/consumer'
|
2019-08-19 17:57:10 -07:00
|
|
|
import btc from 'better-try-catch'
|
2018-01-08 13:06:01 -08:00
|
|
|
|
2018-03-02 08:34:10 -08:00
|
|
|
// const client1= new Consumer({name:'example-consumer1' })
|
2019-08-19 17:57:10 -07:00
|
|
|
const client= new Consumer({path:true, name:'example-consumer', initTimeout:30 })
|
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
|
2019-08-23 15:48:39 -07:00
|
|
|
async function processor (packet) {
|
|
|
|
// console.log('packet being processed at socket', packet)
|
2019-08-20 10:52:59 -07:00
|
|
|
if (packet.cmd) {
|
|
|
|
if (this[packet.cmd]) return await this[packet.cmd](packet)
|
2019-08-23 15:48:39 -07:00
|
|
|
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 }
|
2019-08-20 10:52:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-23 15:48:39 -07:00
|
|
|
client.registerPacketProcessor(processor)
|
|
|
|
|
|
|
|
client.reply = (packet) => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
console.log('generic reply command from server\n',packet.data)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
client.onconnect = (packet) => {
|
2019-08-20 10:52:59 -07:00
|
|
|
return new Promise(resolve => {
|
2019-08-23 15:48:39 -07:00
|
|
|
console.log('on connect server sent command processed here\n',packet.data)
|
2019-08-20 10:52:59 -07:00
|
|
|
resolve()
|
|
|
|
})
|
2018-01-10 15:03:32 -08:00
|
|
|
}
|
|
|
|
|
2019-08-23 15:48:39 -07:00
|
|
|
client.pushed = (packet) => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
console.log('pushed packet\n',packet.status)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-30 16:59:57 -08:00
|
|
|
|
2019-08-20 10:52:59 -07:00
|
|
|
client.on('connection', event => {
|
|
|
|
console.log('============ connection update ============')
|
|
|
|
console.log(event.id)
|
|
|
|
console.log(event.msg)
|
2019-08-23 15:48:39 -07:00
|
|
|
console.log(`Consumer is ${event.connected ? 'connected' : 'disconnected'}`)
|
|
|
|
console.log(`Authenticated? ${event.authenticated || false}`)
|
2019-08-20 10:52:59 -07:00
|
|
|
console.log('======================================')
|
|
|
|
})
|
|
|
|
|
2018-01-08 13:06:01 -08:00
|
|
|
;
|
|
|
|
(async () => {
|
2019-08-20 10:52:59 -07:00
|
|
|
console.log('ready at start',client.ready)
|
2018-02-12 14:41:06 -08:00
|
|
|
// await Promise.all([client1.connect(),client2.connect()])
|
2019-08-19 17:57:10 -07:00
|
|
|
let [err, res] = await btc(client.connect)()
|
|
|
|
if (err) {
|
|
|
|
console.log('error', err)
|
|
|
|
} else {
|
2019-08-23 15:48:39 -07:00
|
|
|
console.log(res)
|
2019-08-20 10:52:59 -07:00
|
|
|
let packet = {name: 'client', cmd:'doit', data:'sent by client'}
|
2019-08-23 15:48:39 -07:00
|
|
|
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('===============')
|
2019-08-19 17:57:10 -07:00
|
|
|
}
|
2018-01-08 13:06:01 -08:00
|
|
|
|
|
|
|
})().catch(err => {
|
|
|
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
|
|
|
})
|