From 0f82f4aedd34930219b6d12837db0ef1772ae78d Mon Sep 17 00:00:00 2001 From: David Kebler Date: Mon, 22 Jan 2018 12:18:34 -0800 Subject: [PATCH] fix app processor property that wasn't getting set --- examples/.gitignore | 1 + examples/client.mjs | 1 - src/consumer.mjs | 6 +++--- src/socket.mjs | 13 ++++++++----- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/.gitignore b/examples/.gitignore index c74d682..b049ffa 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1 +1,2 @@ *.sock +/node_modules/ diff --git a/examples/client.mjs b/examples/client.mjs index 3472883..e577970 100644 --- a/examples/client.mjs +++ b/examples/client.mjs @@ -12,7 +12,6 @@ let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'} let app = { processIt: function processPacket (packet) { console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`) - // console.dir(packet) }, pp: 'processIt' } diff --git a/src/consumer.mjs b/src/consumer.mjs index aff6d6c..cdb4064 100644 --- a/src/consumer.mjs +++ b/src/consumer.mjs @@ -18,6 +18,7 @@ export default class Consumer extends Socket { this._ready = false this.timeout = opts.timeout || 500 this.wait = opts.wait || 5 + this.stream = new JsonStream() // logging this.log_file=opts.log_file || './socket.log' this.log_opts = {streams:[]} @@ -34,12 +35,10 @@ export default class Consumer extends Socket { async connect (app={}) { - this.stream = new JsonStream() - // first set the packet process - this._pp = app.pp || this._pp this.pp = this.pp || this._pp if (Object.keys(app).length === 0) app = this + else app.pp = app.pp || this._pp // set a default processor if none provided if (!app[this.pp]) { this.pp = 'processPacket' // reset in case alt function is missing @@ -90,6 +89,7 @@ export default class Consumer extends Socket { if (packet.ready) { this._ready = true return } + // console.log('consumer processor',app[app.pp]) await app[app.pp].bind(app)(packet) } } diff --git a/src/socket.mjs b/src/socket.mjs index b1e9b64..ba9027b 100644 --- a/src/socket.mjs +++ b/src/socket.mjs @@ -19,7 +19,7 @@ export default class Socket extends Server { this.log_file=opts.log_file || './socket.log' this.log_opts = {streams:[]} this.log_opts.name = opts.name ? opts.name : 'uci-socket' - // this.log_opts.streams.push({level: 'info',path: this.log_file }) + // if (opts.log===1)// this.log_opts.streams.push({level: 'info',path: this.log_file }) if (opts.log) this.log_opts.streams.push({level: 'info',stream: process.stdout}) this.log = bunyan.createLogger(this.log_opts) //binding @@ -29,17 +29,19 @@ export default class Socket extends Server { async create (app={}) { // first set the packet process - this._pp = app.pp || this._pp this.pp = this.pp || this._pp if (Object.keys(app).length === 0) app = this + else app.pp = app.pp || this._pp // set a default processor if none provided - if (!app[this.pp]) { - this.pp = 'processPacket' // reset in case alt function is missing + if (!app[app.pp]) { + app.pp = 'processPacket' // reset in case alt function is missing app.processPacket = async (packet) => { packet.res='echoed' return packet } } + // console.log('socket processing app ',app[app.pp]) + return new Promise( async (resolve,reject) => { ON_DEATH( async () => { @@ -68,7 +70,7 @@ export default class Socket extends Server { reject(err) }) - let [err, res] = await btc(this.listen.bind(this))(this.listen_opts,app) + let [err, res] = await btc(this.listen).bind(this)(this.listen_opts,app) if (err) reject(err) resolve(res) @@ -93,6 +95,7 @@ export default class Socket extends Server { stream.on('message', async function (packet) { // console.log('incoming packet from consumer',packet) + // console.log('socket processing app ',app[app.pp]) socket.write(stream.serialize(await app[app.pp].bind(app)(packet))) })