improved/added example files
parent
c7d25521e8
commit
9d6a562cda
|
@ -2,8 +2,8 @@ import Consumer from '../src/consumer'
|
||||||
|
|
||||||
const USOCKET = __dirname + '/sample.sock'
|
const USOCKET = __dirname + '/sample.sock'
|
||||||
|
|
||||||
const client1= new Consumer(USOCKET, {log:true,name:'example-consumer1' })
|
const client1= new Consumer(USOCKET, {log:false,name:'example-consumer1' })
|
||||||
const client2 = new Consumer(USOCKET, {log:true,name:'example-consumer2'})
|
const client2 = new Consumer(USOCKET, {log:false,name:'example-consumer2'})
|
||||||
|
|
||||||
let packet1 = {name: 'client1', cmd:'doit', data:'data sent by client1'}
|
let packet1 = {name: 'client1', cmd:'doit', data:'data sent by client1'}
|
||||||
let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
|
let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
|
||||||
|
@ -11,16 +11,19 @@ let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
|
||||||
// This is your client handler object waiting on a message to do something
|
// This is your client handler object waiting on a message to do something
|
||||||
let app = {
|
let app = {
|
||||||
processIt: function processPacket (packet) {
|
processIt: function processPacket (packet) {
|
||||||
console.log('your custom processing of incoming packet')
|
console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`)
|
||||||
console.dir(packet)
|
// console.dir(packet)
|
||||||
},
|
},
|
||||||
ucpp: 'processIt'
|
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 () => {
|
(async () => {
|
||||||
|
|
||||||
await Promise.all([client1.connect(app),client2.connect(app)])
|
await Promise.all([client1.connect(),client2.connect(app)])
|
||||||
await Promise.all([client1.send(packet1),client2.send(packet2)])
|
await Promise.all([client1.send(packet1),client2.send(packet2)])
|
||||||
|
|
||||||
})().catch(err => {
|
})().catch(err => {
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import Consumer from '../src/consumer'
|
||||||
|
|
||||||
|
const USOCKET = __dirname + '/sample.sock'
|
||||||
|
|
||||||
|
|
||||||
|
class Client extends Consumer {
|
||||||
|
constructor(path,opts) {
|
||||||
|
super(path,opts)
|
||||||
|
this.pp = 'processIt'
|
||||||
|
}
|
||||||
|
|
||||||
|
async processIt(packet) {
|
||||||
|
console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const client1= new Client(USOCKET, {log:false,name:'example-consumer1' })
|
||||||
|
const client2 = new Client(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'}
|
||||||
|
|
||||||
|
;
|
||||||
|
(async () => {
|
||||||
|
|
||||||
|
await Promise.all([client1.connect(),client2.connect()])
|
||||||
|
await Promise.all([client1.send(packet1),client2.send(packet2)])
|
||||||
|
|
||||||
|
})().catch(err => {
|
||||||
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
})
|
|
@ -5,9 +5,9 @@ const USOCKET = __dirname + '/sample.sock'
|
||||||
;
|
;
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
||||||
class Test {
|
class Test extends Socket {
|
||||||
constructor() {
|
constructor(path,opts) {
|
||||||
this.socket = new Socket(USOCKET,{name:'example-socket'})
|
super(path,opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
async processPacket(packet) {
|
async processPacket(packet) {
|
||||||
|
@ -26,11 +26,10 @@ const USOCKET = __dirname + '/sample.sock'
|
||||||
return(res)
|
return(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() { return this.socket.create(this)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let test = new Test()
|
let test = new Test(USOCKET,{path: USOCKET, name:'example-socket'})
|
||||||
await test.init()
|
await test.create()
|
||||||
|
|
||||||
})().catch(err => {
|
})().catch(err => {
|
||||||
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
|
||||||
|
|
|
@ -9,7 +9,8 @@
|
||||||
"testci": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec --recursive && codecov || true",
|
"testci": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec --recursive && codecov || true",
|
||||||
"s": "node -r @std/esm examples/server | ./node_modules/.bin/bunyan",
|
"s": "node -r @std/esm examples/server | ./node_modules/.bin/bunyan",
|
||||||
"devs": "./node_modules/.bin/nodemon -r @std/esm -e mjs examples/server",
|
"devs": "./node_modules/.bin/nodemon -r @std/esm -e mjs examples/server",
|
||||||
"c": "node -r @std/esm examples/client | ./node_modules/.bin/bunyan -o short"
|
"c": "node -r @std/esm examples/client | ./node_modules/.bin/bunyan -o short",
|
||||||
|
"c2": "node -r @std/esm examples/client2 | ./node_modules/.bin/bunyan -o short"
|
||||||
},
|
},
|
||||||
"author": "David Kebler",
|
"author": "David Kebler",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|
|
@ -30,7 +30,8 @@ export default class Consumer extends Socket {
|
||||||
|
|
||||||
ready() {return this._ready}
|
ready() {return this._ready}
|
||||||
|
|
||||||
async connect () {
|
async connect (app) {
|
||||||
|
if (app) Object.assign(this, app)
|
||||||
this.listen()
|
this.listen()
|
||||||
this.log.info('listening')
|
this.log.info('listening')
|
||||||
|
|
||||||
|
@ -38,7 +39,6 @@ export default class Consumer extends Socket {
|
||||||
|
|
||||||
this.on('error', async (err) => {
|
this.on('error', async (err) => {
|
||||||
if (err.code === 'EISCONN') {
|
if (err.code === 'EISCONN') {
|
||||||
console.log('===============',err)
|
|
||||||
return resolve('ready')
|
return resolve('ready')
|
||||||
}
|
}
|
||||||
return reject(err)
|
return reject(err)
|
||||||
|
|
Loading…
Reference in New Issue