From 609d13cd50fdb1e9797946597675bf4a561f24d5 Mon Sep 17 00:00:00 2001 From: danghongquan Date: Thu, 7 Nov 2024 20:13:37 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9Asession=E7=AE=A1=E7=90=86=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: danghongquan --- Util.js | 1 + session.js | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Util.js b/Util.js index b0374c9..fcd7624 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 752387e..2d5cec9 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 -- Gitee