From 9d6a562cda4bf6e8498c8b2914df2c943d079c9a Mon Sep 17 00:00:00 2001 From: David Kebler Date: Sat, 20 Jan 2018 14:30:21 -0800 Subject: [PATCH] improved/added example files --- examples/client.mjs | 15 +++++++++------ examples/client2.mjs | 31 +++++++++++++++++++++++++++++++ examples/server.mjs | 11 +++++------ package.json | 3 ++- src/consumer.mjs | 4 ++-- 5 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 examples/client2.mjs diff --git a/examples/client.mjs b/examples/client.mjs index 443e11b..3472883 100644 --- a/examples/client.mjs +++ b/examples/client.mjs @@ -2,8 +2,8 @@ import Consumer from '../src/consumer' const USOCKET = __dirname + '/sample.sock' -const client1= new Consumer(USOCKET, {log:true,name:'example-consumer1' }) -const client2 = new Consumer(USOCKET, {log:true,name:'example-consumer2'}) +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'} @@ -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 let app = { processIt: function processPacket (packet) { - console.log('your custom processing of incoming packet') - console.dir(packet) + console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`) + // 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 () => { - 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)]) })().catch(err => { diff --git a/examples/client2.mjs b/examples/client2.mjs new file mode 100644 index 0000000..1291d89 --- /dev/null +++ b/examples/client2.mjs @@ -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) +}) diff --git a/examples/server.mjs b/examples/server.mjs index 05768d8..9608077 100644 --- a/examples/server.mjs +++ b/examples/server.mjs @@ -5,9 +5,9 @@ const USOCKET = __dirname + '/sample.sock' ; (async () => { - class Test { - constructor() { - this.socket = new Socket(USOCKET,{name:'example-socket'}) + class Test extends Socket { + constructor(path,opts) { + super(path,opts) } async processPacket(packet) { @@ -26,11 +26,10 @@ const USOCKET = __dirname + '/sample.sock' return(res) } - async init() { return this.socket.create(this)} } - let test = new Test() - await test.init() + let test = new Test(USOCKET,{path: USOCKET, name:'example-socket'}) + await test.create() })().catch(err => { console.error('FATAL: UNABLE TO START SYSTEM!\n',err) diff --git a/package.json b/package.json index 52cf92b..0bd4b25 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "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", "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", "license": "MIT", diff --git a/src/consumer.mjs b/src/consumer.mjs index 227894a..a2d965b 100644 --- a/src/consumer.mjs +++ b/src/consumer.mjs @@ -30,7 +30,8 @@ export default class Consumer extends Socket { ready() {return this._ready} - async connect () { + async connect (app) { + if (app) Object.assign(this, app) this.listen() this.log.info('listening') @@ -38,7 +39,6 @@ export default class Consumer extends Socket { this.on('error', async (err) => { if (err.code === 'EISCONN') { - console.log('===============',err) return resolve('ready') } return reject(err)