add active getter
emit status active when connect state changes
This commit is contained in:
David Kebler 2019-11-21 09:35:09 -08:00
parent 7502902a51
commit 18a65b42c5
3 changed files with 27 additions and 15 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "@uci/socket", "name": "@uci/socket",
"version": "0.2.21", "version": "0.2.22",
"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": {
@ -43,11 +43,11 @@
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"esm": "^3.2.25", "esm": "^3.2.25",
"mocha": "^6.2.0", "mocha": "^6.2.2",
"nodemon": "^1.19.1" "nodemon": "^2.0.0"
}, },
"dependencies": { "dependencies": {
"@uci-utils/logger": "0.0.15", "@uci-utils/logger": "^0.0.16",
"better-try-catch": "^0.6.2", "better-try-catch": "^0.6.2",
"clone": "^2.1.2", "clone": "^2.1.2",
"death": "^1.1.0", "death": "^1.1.0",

View file

@ -48,8 +48,8 @@ class SocketConsumer extends Socket {
this.opts = opts this.opts = opts
// default is keepAlive true, must set to false to explicitly disable // default is keepAlive true, must set to false to explicitly disable
// if keepAlive is true then consumer will also be reconnecting consumer // if keepAlive is true then consumer will also be reconnecting consumer
this.initTimeout = opts.initTimeout * 1000 || 60000 this.initTimeout = opts.initTimeout==null ? 60000 : opts.initTimeout * 1000
this.retryWait = opts.retryWait * 1000 || 5000 this.retryWait = opts.retryWait==null ? 5000 : opts.retryWait * 1000
this.keepAlive = 'keepAlive' in opts ? opts.keepAlive : true this.keepAlive = 'keepAlive' in opts ? opts.keepAlive : true
this._connected = false this._connected = false
this._authenticated = false this._authenticated = false
@ -61,10 +61,12 @@ class SocketConsumer extends Socket {
this._conAttempt = 1 this._conAttempt = 1
this._aborted = false this._aborted = false
this._reconnect = false this._reconnect = false
this.retryPause = {} // timeout that may need to be cancelled if init timeout throws
// this._packetProcess = this._packetProcess.bind(this) // this._packetProcess = this._packetProcess.bind(this)
} }
get connected() { return this._connected} get connected() { return this._connected}
get active() { return !!this._authenticated }
async connect() { async connect() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -75,14 +77,16 @@ class SocketConsumer extends Socket {
log.debug('first connnect attempt for', this.opts.id) log.debug('first connnect attempt for', this.opts.id)
this.emit('status',{level:'info', msg:'attempting initial connection', id:this.id, opts:this.opts, ready:false}) this.emit('status',{level:'info', msg:'attempting initial connection', id:this.id, opts:this.opts, ready:false})
console.log('TIMEOUT IN SOCKE CONNECT',this.initTimeout)
let initTimeout = {} let initTimeout = {}
if (this.initTimeout > 499) { if (this.initTimeout > 499) {
initTimeout = setTimeout(() => { initTimeout = setTimeout(() => {
this.emit('status',{level:'info', msg:'initial connection timed out', id:this.id, timeout:this.initTimeout, wait:this.retryWait, opts:this.opts, ready:false}) clearTimeout(this.retryPause)
this.emit('status',{level:'error', msg:'initial connection timed out', id:this.id, timeout:this.initTimeout, wait:this.retryWait, opts:this.opts, ready:false})
this.removeAllListeners() this.removeAllListeners()
log.fatal({method:'connect', line:69, opts: this.opts, msg:`unable to initially connect to ${this.opts.name} in ${this.initTimeout/1000} secs no more attempts!`}) log.fatal({method:'connect', line:69, opts: this.opts, msg:`unable to initially connect to ${this.opts.name} in ${this.initTimeout/1000} secs no more attempts!`})
this.stream.removeAllListeners() this.stream.removeAllListeners()
this.destroy()
reject({ opts: this.opts, msg: `unable to connect initially to socket server in ${this.initTimeout/1000} secs, giving up no more attempts`}) reject({ opts: this.opts, msg: `unable to connect initially to socket server in ${this.initTimeout/1000} secs, giving up no more attempts`})
} }
, this.initTimeout) , this.initTimeout)
@ -119,12 +123,15 @@ class SocketConsumer extends Socket {
this.on('data', this.stream.onData) this.on('data', this.stream.onData)
this.stream.once('message', initialHandshake.bind(this)) this.stream.once('message', initialHandshake.bind(this))
log.debug({method:'connect', line:113, msg:'connected waiting for socket ready handshake'}) log.debug({method:'connect', line:113, msg:'connected waiting for socket ready handshake'})
this.emit('status',{level:'debug', msg:'consumer connected'})
} }
const initialErrorHandler = async (err) => { const initialErrorHandler = async (err) => {
log.debug({method:'connect', line:101, error:err, msg:`error during initial connect, trying again in ${this.retryWait/1000} secs`}) let msg = {level:'error', method:'connect', line:101, error:err, msg:`error during initial connect, trying again in ${this.retryWait/1000} secs`}
await pause(this.retryWait) log.error(msg)
super.connect(this.opts) this.emit('status',msg)
let connect = () => { super.connect(this.opts)}
this.retryPause = setTimeout(connect.bind(this),this.retryWait)
} }

View file

@ -72,13 +72,16 @@ export default function socketClass(Server) {
this._authenticate = this._authenticate.bind(this) this._authenticate = this._authenticate.bind(this)
this.close = promisify(this.close).bind(this) this.close = promisify(this.close).bind(this)
log = logger({ log = logger({
package:'@uci/socket',
file: 'src/socket.js', file: 'src/socket.js',
class: 'Socket', class: 'Socket',
name: 'socket',
id: this.id id: this.id
}) })
} // end constructor } // end constructor
get active() { return this.listening }
/** /**
* create - Description * create - Description
* *
@ -127,18 +130,21 @@ export default function socketClass(Server) {
this.errorCount +=1 // log errors here this.errorCount +=1 // log errors here
this.errors.push(err) this.errors.push(err)
if(this.errorCount>2 && this.errorCount<6) { if(this.errorCount>2 && this.errorCount<6) {
let errors= {level:'warning',msg:'something bad maybe going on, 3 errors', errors:this.errors} let errors= {level:'warn',msg:'something bad maybe going on, 3 errors', errors:this.errors}
this.emit('status', errors) this.emit('status', errors)
log.error(errors) log.error(errors)
} }
if(this.errorCount>5) { if(this.errorCount>5) {
let errors = {level:'fatal',msg:'something fatal is going on, 6 errors', errors:this.errors} let errors = {level:'fatal',msg:'something fatal is going on, 6 errors', errors:this.errors}
log.fatal(errors) log.fatal(errors)
this.listening=false
this.emit('status', {active:this.active})
this.emit('status', errors) this.emit('status', errors)
} }
}) })
log.info({method:'create', line:54, msg:'socket server created and listening at', address:this.address()}) log.info({method:'create', line:54, msg:'socket server created and listening at', address:this.address()})
this.on('connection', this._connectionHandler.bind(this)) this.on('connection', this._connectionHandler.bind(this))
this.emit('status',{active:this.active})
resolve(`socket ready and listening at ${this.address().address}:${this.address().port}`) resolve(`socket ready and listening at ${this.address().address}:${this.address().port}`)
}) })
@ -276,7 +282,7 @@ export default function socketClass(Server) {
} }
else { else {
log.info({msg:'client authenticated successfuly', client:client.name, client_id:client.id}) log.info({msg:'client authenticated successfuly', client:client.name, client_id:client.id})
if (this.allowAnonymous) log.warn({msg:'client connected anonymously', client:client.name, client_id:client.id}) if (this.allowAnonymous) log.warn({msg:'socket consumer connected anonymously', consumer:client.name, consumer_id:client.id})
resolve(client.authenticated) resolve(client.authenticated)
} }
} }
@ -308,7 +314,6 @@ export default function socketClass(Server) {
this.clients.push(consumer) // add client to list this.clients.push(consumer) // add client to list
const stream = new JSONStream() const stream = new JSONStream()
consumer.stream = stream consumer.stream = stream
console.log('new consumer connecting', consumer.id, this.clients.length)
consumer.setKeepAlive(this.keepAlive,3000) consumer.setKeepAlive(this.keepAlive,3000)
// add listeners // add listeners