fix app processor property that wasn't getting set

tls
David Kebler 2018-01-22 12:18:34 -08:00
parent 1fe0d71e0e
commit 0f82f4aedd
4 changed files with 12 additions and 9 deletions

1
examples/.gitignore vendored
View File

@ -1 +1,2 @@
*.sock *.sock
/node_modules/

View File

@ -12,7 +12,6 @@ let packet2 = {name: 'client2', cmd:'doit', data:'data sent by client2'}
let app = { let app = {
processIt: function processPacket (packet) { processIt: function processPacket (packet) {
console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`) console.log(`Packet from ${packet.name} Processed by Socket: ${packet.status}`)
// console.dir(packet)
}, },
pp: 'processIt' pp: 'processIt'
} }

View File

@ -18,6 +18,7 @@ export default class Consumer extends Socket {
this._ready = false this._ready = false
this.timeout = opts.timeout || 500 this.timeout = opts.timeout || 500
this.wait = opts.wait || 5 this.wait = opts.wait || 5
this.stream = new JsonStream()
// logging // logging
this.log_file=opts.log_file || './socket.log' this.log_file=opts.log_file || './socket.log'
this.log_opts = {streams:[]} this.log_opts = {streams:[]}
@ -34,12 +35,10 @@ export default class Consumer extends Socket {
async connect (app={}) { async connect (app={}) {
this.stream = new JsonStream()
// first set the packet process // first set the packet process
this._pp = app.pp || this._pp
this.pp = this.pp || this._pp this.pp = this.pp || this._pp
if (Object.keys(app).length === 0) app = this if (Object.keys(app).length === 0) app = this
else app.pp = app.pp || this._pp
// set a default processor if none provided // set a default processor if none provided
if (!app[this.pp]) { if (!app[this.pp]) {
this.pp = 'processPacket' // reset in case alt function is missing this.pp = 'processPacket' // reset in case alt function is missing
@ -90,6 +89,7 @@ export default class Consumer extends Socket {
if (packet.ready) { if (packet.ready) {
this._ready = true this._ready = true
return } return }
// console.log('consumer processor',app[app.pp])
await app[app.pp].bind(app)(packet) await app[app.pp].bind(app)(packet)
} }
} }

View File

@ -19,7 +19,7 @@ export default class Socket extends Server {
this.log_file=opts.log_file || './socket.log' this.log_file=opts.log_file || './socket.log'
this.log_opts = {streams:[]} this.log_opts = {streams:[]}
this.log_opts.name = opts.name ? opts.name : 'uci-socket' 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}) if (opts.log) this.log_opts.streams.push({level: 'info',stream: process.stdout})
this.log = bunyan.createLogger(this.log_opts) this.log = bunyan.createLogger(this.log_opts)
//binding //binding
@ -29,17 +29,19 @@ export default class Socket extends Server {
async create (app={}) { async create (app={}) {
// first set the packet process // first set the packet process
this._pp = app.pp || this._pp
this.pp = this.pp || this._pp this.pp = this.pp || this._pp
if (Object.keys(app).length === 0) app = this if (Object.keys(app).length === 0) app = this
else app.pp = app.pp || this._pp
// set a default processor if none provided // set a default processor if none provided
if (!app[this.pp]) { if (!app[app.pp]) {
this.pp = 'processPacket' // reset in case alt function is missing app.pp = 'processPacket' // reset in case alt function is missing
app.processPacket = async (packet) => { app.processPacket = async (packet) => {
packet.res='echoed' packet.res='echoed'
return packet } return packet }
} }
// console.log('socket processing app ',app[app.pp])
return new Promise( async (resolve,reject) => { return new Promise( async (resolve,reject) => {
ON_DEATH( async () => { ON_DEATH( async () => {
@ -68,7 +70,7 @@ export default class Socket extends Server {
reject(err) 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) if (err) reject(err)
resolve(res) resolve(res)
@ -93,6 +95,7 @@ export default class Socket extends Server {
stream.on('message', async function (packet) { stream.on('message', async function (packet) {
// console.log('incoming packet from consumer',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))) socket.write(stream.serialize(await app[app.pp].bind(app)(packet)))
}) })