0.1.30 add consumer connection bubble up listener for each socket

add mqtt s vs c option
master
David Kebler 2019-09-16 18:05:03 -07:00
parent 23ea81c9d9
commit ec311b51df
2 changed files with 15 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci/base", "name": "@uci/base",
"version": "0.1.29", "version": "0.1.30",
"description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes", "description": "Multi type and transport JSON packet communication base class. Used in UCI extended classes",
"main": "src/base", "main": "src/base",
"scripts": { "scripts": {

View File

@ -23,7 +23,7 @@ import EventEmitter from 'events'
// Internal dependencies // Internal dependencies
import { processor, defaultCmds, namespaces } from './processing' import { processor, defaultCmds, namespaces } from './processing'
// Useful Constants // Constants
const SOCKET_INFO_KEYS = ['name', 'type', 'transport'] const SOCKET_INFO_KEYS = ['name', 'type', 'transport']
const TRANSLATE = { const TRANSLATE = {
n: 'Named Pipe', n: 'Named Pipe',
@ -167,6 +167,7 @@ class Base extends EventEmitter {
break break
case 'm': case 'm':
if (type === 'p') type = 'c' if (type === 'p') type = 'c'
options.client = type==='c' ? true : false
options.connect = options.connect || {} options.connect = options.connect || {}
if (options.host) options.connect.host = options.host if (options.host) options.connect.host = options.host
if (options.port) options.connect.port = options.port if (options.port) options.connect.port = options.port
@ -183,10 +184,18 @@ class Base extends EventEmitter {
this._socket[name].transport = transport this._socket[name].transport = transport
this._socket[name]._packetProcess = this._packetProcess.bind(this, name) this._socket[name]._packetProcess = this._packetProcess.bind(this, name)
// bubble up events from sockets // bubble up events from sockets to instance
this._socket[name].on('status', ev => { const EVENTS=['status','consumer-connection'] // that should emit up from socket to instance
ev.socketName=name EVENTS.forEach(event => {
this.emit('status', ev) this._socket[name].on(event, obj => {
if (Object.prototype.toString.call(obj) !== '[object Object]') {
let data=obj
obj = {}
obj.data = data
}
obj.socketName = name
this.emit(event,obj)
})
}) })
if (type==='c') { if (type==='c') {