chore(Hub): prevent onClose() multiple times
This commit is contained in:
parent
50719fa5b5
commit
f49e0aabf6
@ -1 +1 @@
|
||||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});exports.Hub=undefined;var _net=require('net');var _net2=_interopRequireDefault(_net);var _winston=require('winston');var _winston2=_interopRequireDefault(_winston);var _socket=require('./socket');var _profile=require('./profile');var _balancer=require('./balancer');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const nextId=function(){let i=0;return()=>{if(i>Number.MAX_SAFE_INTEGER-1){i=0}return++i}}();class Hub{constructor(){this._hub=null;this._sockets=[];this._hub=_net2.default.createServer();this._hub.on('close',this.onClose.bind(this));this._hub.on('connection',this.onConnect.bind(this));this.onSocketClose=this.onSocketClose.bind(this)}onClose(){console.info('==> [hub] shutdown');if(__IS_CLIENT__){_balancer.Balancer.destroy();console.info('==> [balancer] stopped')}if(__PROFILE__){console.info('==> [profile] saving...');_profile.Profile.save();_profile.Profile.stop();console.info('==> [profile] stopped')}}onSocketClose(socket){this._sockets=this._sockets.filter(({id})=>id!==socket.id);_profile.Profile.connections=this._sockets.length}onConnect(socket){const id=nextId();const instance=new _socket.Socket({id,socket,onClose:this.onSocketClose});this._sockets.push(instance);_winston2.default.info(`[hub] [${socket.remoteAddress}:${socket.remotePort}] connected`);_profile.Profile.connections+=1}run(){const options={host:__LOCAL_HOST__,port:__LOCAL_PORT__};this._hub.listen(options,()=>{console.info('==> [hub] use configuration:');console.info(JSON.stringify(__ALL_CONFIG__,null,' '));console.info(`==> [hub] is running as: ${__IS_SERVER__?'Server':'Client'}`);console.info('==> [hub] is listening on:',this._hub.address());if(__IS_CLIENT__){console.info('==> [balancer] started');_balancer.Balancer.start(__SERVERS__)}if(__PROFILE__){console.info('==> [profile] started');_profile.Profile.start()}})}terminate(){this._hub.close();this.onClose()}}exports.Hub=Hub;
|
||||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});exports.Hub=undefined;var _net=require('net');var _net2=_interopRequireDefault(_net);var _winston=require('winston');var _winston2=_interopRequireDefault(_winston);var _socket=require('./socket');var _profile=require('./profile');var _balancer=require('./balancer');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const nextId=function(){let i=0;return()=>{if(i>Number.MAX_SAFE_INTEGER-1){i=0}return++i}}();class Hub{constructor(){this._hub=null;this._sockets=[];this._isClosed=false;this._hub=_net2.default.createServer();this._hub.on('close',this.onClose.bind(this));this._hub.on('connection',this.onConnect.bind(this));this.onSocketClose=this.onSocketClose.bind(this)}onClose(){if(!this._isClosed){console.info('==> [hub] shutdown');if(__IS_CLIENT__){_balancer.Balancer.destroy();console.info('==> [balancer] stopped')}if(__PROFILE__){console.info('==> [profile] saving...');_profile.Profile.save();_profile.Profile.stop();console.info('==> [profile] stopped')}this._isClosed=true}}onSocketClose(socket){this._sockets=this._sockets.filter(({id})=>id!==socket.id);_profile.Profile.connections=this._sockets.length}onConnect(socket){const id=nextId();const instance=new _socket.Socket({id,socket,onClose:this.onSocketClose});this._sockets.push(instance);_winston2.default.info(`[hub] [${socket.remoteAddress}:${socket.remotePort}] connected`);_profile.Profile.connections+=1}run(){const options={host:__LOCAL_HOST__,port:__LOCAL_PORT__};this._hub.listen(options,()=>{console.info('==> [hub] use configuration:');console.info(JSON.stringify(__ALL_CONFIG__,null,' '));console.info(`==> [hub] is running as: ${__IS_SERVER__?'Server':'Client'}`);console.info('==> [hub] is listening on:',this._hub.address());if(__IS_CLIENT__){console.info('==> [balancer] started');_balancer.Balancer.start(__SERVERS__)}if(__PROFILE__){console.info('==> [profile] started');_profile.Profile.start()}})}terminate(){this._hub.close();this.onClose()}}exports.Hub=Hub;
|
@ -20,6 +20,8 @@ export class Hub {
|
||||
|
||||
_sockets = []; // instances of our class Socket
|
||||
|
||||
_isClosed = false;
|
||||
|
||||
constructor() {
|
||||
this._hub = net.createServer();
|
||||
this._hub.on('close', this.onClose.bind(this));
|
||||
@ -28,16 +30,19 @@ export class Hub {
|
||||
}
|
||||
|
||||
onClose() {
|
||||
console.info('==> [hub] shutdown');
|
||||
if (__IS_CLIENT__) {
|
||||
Balancer.destroy();
|
||||
console.info('==> [balancer] stopped');
|
||||
}
|
||||
if (__PROFILE__) {
|
||||
console.info('==> [profile] saving...');
|
||||
Profile.save();
|
||||
Profile.stop();
|
||||
console.info('==> [profile] stopped');
|
||||
if (!this._isClosed) {
|
||||
console.info('==> [hub] shutdown');
|
||||
if (__IS_CLIENT__) {
|
||||
Balancer.destroy();
|
||||
console.info('==> [balancer] stopped');
|
||||
}
|
||||
if (__PROFILE__) {
|
||||
console.info('==> [profile] saving...');
|
||||
Profile.save();
|
||||
Profile.stop();
|
||||
console.info('==> [profile] stopped');
|
||||
}
|
||||
this._isClosed = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user