diff --git a/package.json b/package.json index 58bbb82..ab22970 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci/socket", - "version": "0.2.33", + "version": "0.3.0", "description": "JSON packet intra(named)/inter(TCP) host communication over socket", "main": "src", "scripts": { @@ -43,8 +43,8 @@ "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "esm": "^3.2.25", - "mocha": "^6.2.2", - "nodemon": "^2.0.0" + "mocha": "^7.1.0", + "nodemon": "^2.0.2" }, "dependencies": { "@uci-utils/logger": "^0.0.16", @@ -52,7 +52,7 @@ "clone": "^2.1.2", "death": "^1.1.0", "delay": "^4.3.0", - "make-dir": "^3.0.0", + "make-dir": "^3.0.2", "p-reflect": "^2.1.0" } } diff --git a/src/consumer.js b/src/consumer.js index 4daa344..f9853c7 100644 --- a/src/consumer.js +++ b/src/consumer.js @@ -70,7 +70,6 @@ class SocketConsumer extends Socket { this._connection = 'offline' this._first = true // first connection or not this._pingTimeout // value sent from socket upon connect - console.log('consumer.js @uci/socket package tag 0.2.33') } get connected() { return this._connected} @@ -190,7 +189,6 @@ class SocketConsumer extends Socket { if (err) resolve({error: 'unable to serialize packet for sending',packet: packet}) if (this.active && this.writable) { - console.log('writable can write') let res2 = await this.__write(res) if (res2.error) resolve(res2) // if no write error then wait for send response @@ -295,7 +293,6 @@ async function handshake (packet) { this.notify('handshake') const authPacket = Object.assign(this._authenticate() || {}, {_authenticate:true, data:this._data}) // console.log('----------------authentication packet---------------',authPacket) - console.log('authenticate writable',this.writable) let res ={} if (!this.writable) res.error ='socket stream not writable' else res = await this._authenticateSend(authPacket) diff --git a/src/socket-class.js b/src/socket-class.js index 80ee406..d24d0f5 100644 --- a/src/socket-class.js +++ b/src/socket-class.js @@ -66,7 +66,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.conPackets = opts.conPackets || [opts.conPacket] this.opts = opts // for use to recover from selected errors this.errorCount = 0 //self bindings @@ -80,7 +80,6 @@ export default function socketClass(Server) { class: 'Socket', id: this.id }) - console.log('socket-class: @uci/socket package tag 0.2.33') } // end constructor @@ -113,7 +112,7 @@ export default function socketClass(Server) { log.info({method:'create', line:99, res: res, socket: this.opts.path, msg:'socket already exists.....deleted'}) // try again this.removeAllListeners('listening') - return await this.create() + resolve(await this.create()) } log.error({method:'create', line:102, err: err, msg:'error deleting socket. Can not establish a socket'}) } @@ -123,7 +122,7 @@ export default function socketClass(Server) { await mkdir(path.dirname(this.opts.path)) log.debug({method:'create', line:109, socket: this.opts.path, msg:'directory created'}) this.removeAllListeners('listening') - return await this.create() + resolve(await this.create()) } // otherwise fatally exit log.error({method:'create', line:113, err:err, opts:this.opts, msg:`error creating socket server ${this.name}`}) @@ -251,7 +250,7 @@ export default function socketClass(Server) { if (opts.consumer) opts.consumers = [opts.consumer] consumers = Array.from(this.consumers).filter(([sid,consumer]) => opts.consumers.some(con=> { - console.log(consumer.sid,consumer.data,con) + // console.log('filtering consumers', consumer.sid,consumer.data,con) return ( con === ( (consumer.data ||{}).name || (consumer.data ||{}).id ) || con.sid=== sid || @@ -412,18 +411,15 @@ export default function socketClass(Server) { // all's set enable main incoming message processor stream.on('message', messageProcess.bind(this, consumer)) - 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'}) - // FIXME, don't use forEach and use promise map. - 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 - } - }) + if (this.conPackets.length) { + // console.log({conPackets: this.conPackets, msg:'pushing preset packets to just connected consumer'}) + for (let packet of this.conPackets) { + packet._header = {type:'on connection packet', id: 'pushed'} + await 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('connection:consumer',{state:'connected', msg:`consumer ${(consumer.data ||{}).name} connected and authenticated to socket ${this.id}`, name:(consumer.data ||{}).name ||(consumer.data ||{}).id || consumer.sid,