add active getter
emit status active when connect state changes
master
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",
"version": "0.2.21",
"version": "0.2.22",
"description": "JSON packet intra(named)/inter(TCP) host communication over socket",
"main": "src",
"scripts": {
@ -43,11 +43,11 @@
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"esm": "^3.2.25",
"mocha": "^6.2.0",
"nodemon": "^1.19.1"
"mocha": "^6.2.2",
"nodemon": "^2.0.0"
},
"dependencies": {
"@uci-utils/logger": "0.0.15",
"@uci-utils/logger": "^0.0.16",
"better-try-catch": "^0.6.2",
"clone": "^2.1.2",
"death": "^1.1.0",

View File

@ -48,8 +48,8 @@ class SocketConsumer extends Socket {
this.opts = opts
// default is keepAlive true, must set to false to explicitly disable
// if keepAlive is true then consumer will also be reconnecting consumer
this.initTimeout = opts.initTimeout * 1000 || 60000
this.retryWait = opts.retryWait * 1000 || 5000
this.initTimeout = opts.initTimeout==null ? 60000 : opts.initTimeout * 1000
this.retryWait = opts.retryWait==null ? 5000 : opts.retryWait * 1000
this.keepAlive = 'keepAlive' in opts ? opts.keepAlive : true
this._connected = false
this._authenticated = false
@ -61,10 +61,12 @@ class SocketConsumer extends Socket {
this._conAttempt = 1
this._aborted = false
this._reconnect = false
this.retryPause = {} // timeout that may need to be cancelled if init timeout throws
// this._packetProcess = this._packetProcess.bind(this)
}
get connected() { return this._connected}
get active() { return !!this._authenticated }
async connect() {
return new Promise((resolve, reject) => {
@ -75,14 +77,16 @@ class SocketConsumer extends Socket {
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})
console.log('TIMEOUT IN SOCKE CONNECT',this.initTimeout)
let initTimeout = {}
if (this.initTimeout > 499) {
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()
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.destroy()
reject({ opts: this.opts, msg: `unable to connect initially to socket server in ${this.initTimeout/1000} secs, giving up no more attempts`})
}
, this.initTimeout)
@ -119,12 +123,15 @@ class SocketConsumer extends Socket {
this.on('data', this.stream.onData)
this.stream.once('message', initialHandshake.bind(this))
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) => {
log.debug({method:'connect', line:101, error:err, msg:`error during initial connect, trying again in ${this.retryWait/1000} secs`})
await pause(this.retryWait)
super.connect(this.opts)
let msg = {level:'error', method:'connect', line:101, error:err, msg:`error during initial connect, trying again in ${this.retryWait/1000} secs`}
log.error(msg)
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.close = promisify(this.close).bind(this)
log = logger({
package:'@uci/socket',
file: 'src/socket.js',
class: 'Socket',
name: 'socket',
id: this.id
})
} // end constructor
get active() { return this.listening }
/**
* create - Description
*
@ -127,18 +130,21 @@ export default function socketClass(Server) {
this.errorCount +=1 // log errors here
this.errors.push(err)
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)
log.error(errors)
}
if(this.errorCount>5) {
let errors = {level:'fatal',msg:'something fatal is going on, 6 errors', errors:this.errors}
log.fatal(errors)
this.listening=false
this.emit('status', {active:this.active})
this.emit('status', errors)
}
})
log.info({method:'create', line:54, msg:'socket server created and listening at', address:this.address()})
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}`)
})
@ -276,7 +282,7 @@ export default function socketClass(Server) {
}
else {
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)
}
}
@ -308,7 +314,6 @@ export default function socketClass(Server) {
this.clients.push(consumer) // add client to list
const stream = new JSONStream()
consumer.stream = stream
console.log('new consumer connecting', consumer.id, this.clients.length)
consumer.setKeepAlive(this.keepAlive,3000)
// add listeners