diff --git a/src/consumer.mjs b/src/consumer.mjs index e57b098..71aabd8 100644 --- a/src/consumer.mjs +++ b/src/consumer.mjs @@ -5,13 +5,14 @@ import bunyan from 'bunyan' import JsonStream from './json' export default class Consumer extends Socket { - constructor (path, opts={}) { + constructor (path={}, opts={}) { super() // set or tcp socket if (typeof(path)!=='string') { opts = path - this.host = opts.host || '127.0.0.1' // TODO log a warning about host on same machine - this.port = opts.port || 8080 + if (!path.host || !path.port) opts = path + this.host = path.host || '127.0.0.1' // TODO log a warning about host on same machine + this.port = path.port || 8080 } else this.path = path this.packet = { _process: async (packet) => { diff --git a/src/socket.mjs b/src/socket.mjs index 59d6279..98455e5 100644 --- a/src/socket.mjs +++ b/src/socket.mjs @@ -7,27 +7,27 @@ import bunyan from 'bunyan' import JsonStream from './json' export default class Socket extends Server { - constructor (path, opts={}) { + constructor (path={},opts={}) { super() // set or tcp socket if (typeof(path)!=='string') { - opts = path - this.listen_opts = { host: opts.host || '0.0.0.0', port: opts.port || 8080} + if (!path.host || !path.port) opts = path + this.listen_opts = { host: path.host || '0.0.0.0', port: path.port || 8080} } else this.listen_opts = { path: path } - // default packet processing + // default packet processing - simple echo server this.packet = { _process: (packet) => { packet.res='echoed' return packet } } - // logging + // Change to environment based configuration for logger this.log_file=opts.log_file || './socket.log' this.log_opts = {streams:[]} this.log_opts.name = opts.name ? opts.name : 'uci-socket' // 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 + //self binding this.listen = this.listen.bind(this) this.create = this.create.bind(this)