improved/added example files

tls
David Kebler 2018-01-20 14:30:21 -08:00
parent c7d25521e8
commit 9d6a562cda
5 changed files with 49 additions and 15 deletions

View File

@ -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 => {

31
examples/client2.mjs Normal file
View File

@ -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)
})

View File

@ -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)

View File

@ -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",

View File

@ -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)