alter to passed options to class to better support both tcp and unix sockets with same class
parent
bb0ca93e43
commit
2d38eab240
|
@ -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) => {
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue