diff --git a/Util.js b/Util.js index b0374c9ba9aa696fa234b4a1ee447887f227373b..fcd76240db080a276a76c5b360a8f7c4a138f22a 100644 --- a/Util.js +++ b/Util.js @@ -22,6 +22,7 @@ class AuthMsg { static SESSION_ID_BYTE = 1; //sessionId占字节 static SESSION_TBYTE = 4; //session占字节 static SW_PORT = 19099; //websockets端口 + static SESSION_LIMIT = 256; //最大并发数(最多可同时申请256个session) } class Utils { diff --git a/session.js b/session.js index 752387e33ee40ba35c8b66cfb12ea2670a9aabcc..2d5cec948a26d768e73e82f6887a3b0afc718109 100644 --- a/session.js +++ b/session.js @@ -15,23 +15,15 @@ const Utils = require('./Util.js'); const TypeManage = require('./typeManage.js'); class SessionJs { - static sessionMap = new Map([ - [1, null], - [2, null], - [3, null], - [4, null], - [5, null], - [6, null], - [7, null], - [8, null] - ]); + static sessionMap = getSessionMap(); static applySession = function (ws, decoderMessage) { // 防止连续申请session - if(ws.userData && ws.userData.sessionId){ + if (ws.userData && ws.userData.sessionId) { return; }; let sendSessionMsg = {}; if (decoderMessage.cmd === Utils.AuthMsg.LOGIN_CMD) { + ws.userData = {}; for (let key of SessionJs.sessionMap.keys()) { if (SessionJs.sessionMap.get(key) === null) { // 将sessionId保存在对应ws的userData对象中,在关闭会话的时候要用 @@ -58,6 +50,14 @@ class SessionJs { }; }; +function getSessionMap(){ + const getSessionMap = new Map(); + for (let index = 1; index <= Utils.AuthMsg.SESSION_LIMIT; index++) { + getSessionMap.set(index,null); + } + return getSessionMap; +} + module.exports = { SessionJs