0.2.31 support for multiple conPackets as an array

master
David Kebler 2020-01-18 22:34:38 -08:00
parent 65169e3ded
commit 42d4b54008
2 changed files with 13 additions and 6 deletions

View File

@ -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": {

View File

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