diff options
author | marzavec <admin@marzavec.com> | 2018-04-29 07:29:38 +0200 |
---|---|---|
committer | marzavec <admin@marzavec.com> | 2018-04-29 07:29:46 +0200 |
commit | 8820968c7378b03af57abce3d3a522cff318c9b9 (patch) | |
tree | 41bb23bdbddd484be3567732c531c11e31b22c32 /server/src/core | |
parent | added software link to readme (diff) | |
download | hackchat-8820968c7378b03af57abce3d3a522cff318c9b9.tar.gz hackchat-8820968c7378b03af57abce3d3a522cff318c9b9.zip |
misc server changes and new modules
Diffstat (limited to 'server/src/core')
-rw-r--r-- | server/src/core/server.js | 69 |
1 files changed, 56 insertions, 13 deletions
diff --git a/server/src/core/server.js b/server/src/core/server.js index 753ed70..9bea738 100644 --- a/server/src/core/server.js +++ b/server/src/core/server.js @@ -14,6 +14,7 @@ const socketReady = require('ws').OPEN; const crypto = require('crypto'); const ipSalt = (Math.random().toString(36).substring(2, 16) + Math.random().toString(36).substring(2, (Math.random() * 16))).repeat(16); const Police = require('./rateLimiter'); +const pulseSpeed = 16000; // ping all clients every X ms class server extends wsServer { /** @@ -27,6 +28,9 @@ class server extends wsServer { this._core = core; this._police = new Police(); this._cmdBlacklist = {}; + this._heartBeat = setInterval(((data) => { + this.beatHeart(); + }).bind(this), pulseSpeed); this.on('error', (err) => { this.handleError('server', err); @@ -38,6 +42,26 @@ class server extends wsServer { } /** + * Send empty `ping` frame to each client + * + */ + beatHeart () { + let targetSockets = this.findSockets({}); + + if (targetSockets.length === 0) { + return; + } + + for (let i = 0, l = targetSockets.length; i < l; i++) { + try { + if (targetSockets[i].readyState === socketReady) { + targetSockets[i].ping(); + } + } catch (e) { } + } + } + + /** * Bind listeners for the new socket created on connection to this class * * @param {Object} socket New socket object @@ -120,16 +144,7 @@ class server extends wsServer { * @param {Object} socket Closing socket object */ handleClose (socket) { - try { - if (socket.channel) { - this.broadcast({ - cmd: 'onlineRemove', - nick: socket.nick - }, { channel: socket.channel }); - } - } catch (err) { - console.log(`Server, handle close event error: ${err}`); - } + this._core.commands.handleCommand(this, socket, { cmd: 'disconnect' }); } /** @@ -206,9 +221,37 @@ class server extends wsServer { for ( let socket of this.clients ) { curMatch = 0; - for( let i = 0; i < reqCount; i++ ) { - if (typeof socket[filterAttribs[i]] !== 'undefined' && socket[filterAttribs[i]] === filter[filterAttribs[i]]) - curMatch++; + for (let i = 0; i < reqCount; i++) { + if (typeof socket[filterAttribs[i]] !== 'undefined') { + switch(typeof filter[filterAttribs[i]]) { + case 'object': { + if (Array.isArray(filter[filterAttribs[i]])) { + if (filter[filterAttribs[i]].indexOf(socket[filterAttribs[i]]) !== -1) { + curMatch++; + } + } else { + if (socket[filterAttribs[i]] === filter[filterAttribs[i]]) { + curMatch++; + } + } + break; + } + + case 'function': { + if (filter[filterAttribs[i]](socket[filterAttribs[i]])) { + curMatch++; + } + break; + } + + default: { + if (socket[filterAttribs[i]] === filter[filterAttribs[i]]) { + curMatch++; + } + break; + } + } + } } if (curMatch === reqCount) { |