0.2.31 support for multiple conPackets as an array
parent
65169e3ded
commit
42d4b54008
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@uci/socket",
|
"name": "@uci/socket",
|
||||||
"version": "0.2.30",
|
"version": "0.2.31",
|
||||||
"description": "JSON packet intra(named)/inter(TCP) host communication over socket",
|
"description": "JSON packet intra(named)/inter(TCP) host communication over socket",
|
||||||
"main": "src",
|
"main": "src",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -64,6 +64,7 @@ export default function socketClass(Server) {
|
||||||
this.pingInterval = opts.pingInterval === false ? 0 : (opts.pingInterval * 1000 || 5000)
|
this.pingInterval = opts.pingInterval === false ? 0 : (opts.pingInterval * 1000 || 5000)
|
||||||
this.consumers = new Map() // track consumers (i.e. clients) TODO use a Map
|
this.consumers = new Map() // track consumers (i.e. clients) TODO use a Map
|
||||||
this.nextConsumerID = 0 // incrementer for default initial consumer ID
|
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.opts = opts // for use to recover from selected errors
|
||||||
this.errorCount = 0
|
this.errorCount = 0
|
||||||
//self bindings
|
//self bindings
|
||||||
|
@ -237,11 +238,12 @@ export default function socketClass(Server) {
|
||||||
* @param {string} id the header id string of the pushed packet, default: 'pushed'
|
* @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) {
|
async push(packet={},id) {
|
||||||
packet._header = {id: id || 'pushed'}
|
packet._header = {id: id || 'pushed'}
|
||||||
if (this.consumers.size > 0) {
|
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'})
|
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 => {
|
this.consumers.forEach(async consumer => {
|
||||||
if (consumer.writable) {
|
if (consumer.writable) {
|
||||||
let [err] = await btc(this._send)(consumer,packet)
|
let [err] = await btc(this._send)(consumer,packet)
|
||||||
|
@ -291,7 +293,7 @@ export default function socketClass(Server) {
|
||||||
packet.authenticated = consumer.authenticated
|
packet.authenticated = consumer.authenticated
|
||||||
packet.reason = err || null
|
packet.reason = err || null
|
||||||
log.debug({msg:'sending authorization result to consumer', packet:packet})
|
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) {
|
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})
|
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)
|
reject(packet.reason)
|
||||||
|
@ -378,10 +380,15 @@ export default function socketClass(Server) {
|
||||||
// all's set enable main incoming message processor
|
// all's set enable main incoming message processor
|
||||||
stream.on('message', messageProcess.bind(this, consumer))
|
stream.on('message', messageProcess.bind(this, consumer))
|
||||||
|
|
||||||
if (this.opts.conPacket) {
|
if (this.conPackets) {
|
||||||
this.opts.conPacket._header = { id: 'pushed' }
|
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'})
|
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})
|
this.emit('log',{level:'info', msg:'a consumer connected and authenticated', name:consumer.name, id:consumer.id})
|
||||||
|
|
Loading…
Reference in New Issue