alter to passed options to class to better support both tcp and unix sockets with same class

tls
David Kebler 2018-01-29 21:38:46 -08:00
parent bb0ca93e43
commit 2d38eab240
2 changed files with 10 additions and 9 deletions

View File

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

View File

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