add active getter and emit active and error status when need be
master
David Kebler 2019-11-21 09:42:22 -08:00
parent 88c61f0895
commit 4fcdc4166a
2 changed files with 19 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@uci/websocket", "name": "@uci/websocket",
"version": "0.3.9", "version": "0.3.10",
"description": "JSON packet host websocket server", "description": "JSON packet host websocket server",
"main": "src", "main": "src",
"scripts": { "scripts": {
@ -33,15 +33,15 @@
"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",
"await-to-js": "^2.1.1", "await-to-js": "^2.1.1",
"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",
"ws": "^7.1.2" "ws": "^7.2.0"
} }
} }

View File

@ -41,6 +41,7 @@ class Socket extends 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/websocket',
file: 'src/socket.js', file: 'src/socket.js',
class: 'Socket', class: 'Socket',
name: 'websocket', name: 'websocket',
@ -48,6 +49,8 @@ class Socket extends Server {
}) })
} // end constructor } // end constructor
get active() { return this.listening }
/** /**
* create - Description * create - Description
* *
@ -78,12 +81,19 @@ class Socket extends Server {
this.on('error', err => { this.on('error', err => {
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.emit('warn', {msg:'something bad maybe going on, 3 errors', errors:this.errors}) if(this.errorCount>2) this.emit('status', {level:'warn',msg:'something bad maybe going on, 3 errors', errors:this.errors})
if(this.errorCount>5) this.emit('fatal', {msg:'something fatal is going on, 6 errors', errors:this.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)
}
}) })
this.wss = new WSS({server:this}) this.wss = new WSS({server:this})
this.wss.on('error', err => {this.emit('error', err)}) // bubble up errors this.wss.on('error', err => {this.emit('error', err)}) // bubble up errors
this.wss.on('connection', this._connectionHandler.bind(this)) this.wss.on('connection', this._connectionHandler.bind(this))
this.emit('status',{active:this.active})
resolve(`websocket ready and listening at ${this.address().address}:${this.address().port}`) resolve(`websocket ready and listening at ${this.address().address}:${this.address().port}`)
}) })
super.listen(this.opts) super.listen(this.opts)
@ -189,7 +199,7 @@ class Socket extends Server {
} }
else { else {
log.info({msg:'consumer authenticated successfuly', consumer:consumer.name, consumer_id:consumer.id}) log.info({msg:'consumer authenticated successfuly', consumer:consumer.name, consumer_id:consumer.id})
if (this.allowAnonymous) log.warn({msg:'consumer connected anonymously', consumer:consumer.name, consumer_id:consumer.id}) if (this.allowAnonymous) log.warn({msg:'web consumer connected anonymously', consumer:consumer.name, consumer_id:consumer.id})
resolve(consumer.authenticated) resolve(consumer.authenticated)
} }
} }
@ -265,7 +275,7 @@ class Socket extends Server {
res = { error: 'Could not JSON parse packet', packet:strPacket } res = { error: 'Could not JSON parse packet', packet:strPacket }
} }
else { else {
log.warn({method:'_listen', line:266, packet:packet, msg:'parsed packet'}) log.debug({method:'_listen', line:266, packet:packet, msg:'parsed packet'})
res = (await this._packetProcess(clone(packet))) || {} res = (await this._packetProcess(clone(packet))) || {}
if (Object.keys(res).length === 0) if (Object.keys(res).length === 0)
res = { res = {