diff --git a/package.json b/package.json index 9c8431b..bd622fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci/socket", - "version": "0.2.30", + "version": "0.2.31", "description": "JSON packet intra(named)/inter(TCP) host communication over socket", "main": "src", "scripts": { diff --git a/src/socket-class.js b/src/socket-class.js index a172aa7..f911a70 100644 --- a/src/socket-class.js +++ b/src/socket-class.js @@ -64,6 +64,7 @@ export default function socketClass(Server) { this.pingInterval = opts.pingInterval === false ? 0 : (opts.pingInterval * 1000 || 5000) this.consumers = new Map() // track consumers (i.e. clients) TODO use a Map this.nextConsumerID = 0 // incrementer for default initial consumer ID + this.conPackets = opts.conPackets || opts.conPacket this.opts = opts // for use to recover from selected errors this.errorCount = 0 //self bindings @@ -237,11 +238,12 @@ export default function socketClass(Server) { * @param {string} id the header id string of the pushed packet, default: 'pushed' * */ + // TODO push to a specific set of consumers only async push(packet={},id) { packet._header = {id: id || 'pushed'} if (this.consumers.size > 0) { log.trace({method:'push', line:142, id:packet._header.id, opts: this.opts, packet: packet, msg:'pushing a packet to all connected consumers'}) - // TODO should do a map and single promise + // FIXME async with forEach not supported, use loop or map this.consumers.forEach(async consumer => { if (consumer.writable) { let [err] = await btc(this._send)(consumer,packet) @@ -291,7 +293,7 @@ export default function socketClass(Server) { packet.authenticated = consumer.authenticated packet.reason = err || null log.debug({msg:'sending authorization result to consumer', packet:packet}) - await this._send(consumer,packet) // send either way + this._send(consumer,packet) // send either way if (err && !this.allowAnonymous) { log.info({msg:`consumer ${consumer.data.name} authentication failed`, name:consumer.name, id:consumer.id, data:consumer.data, consumer_sid:consumer.sid, reason:err}) reject(packet.reason) @@ -378,10 +380,15 @@ export default function socketClass(Server) { // all's set enable main incoming message processor stream.on('message', messageProcess.bind(this, consumer)) - if (this.opts.conPacket) { - this.opts.conPacket._header = { id: 'pushed' } + if (this.conPackets) { + this.conPackets = Array.isArray(this.conPackets) ? this.conPackets : [this.conPackets] log.debug({method:'_listen', line:171, conPacket: this.opts.conPacket, msg:'pushing a preset command to just connected consumer'}) - this._send(consumer,this.opts.conPacket) // send a packet command on to consumer on connection + this.conPackets.forEach(packet => { + if (packet) { + packet._header = {type:'on connection packet', id: 'pushed'} + this._send(consumer,packet) // send a packet command on to consumer on connection + } + }) } this.emit('log',{level:'info', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id})