From 4561c498a2ecf15f426b85dc9597d4f4a28b4b25 Mon Sep 17 00:00:00 2001 From: satori Date: Sat, 4 Dec 2021 05:13:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=B8=B8=E5=AE=A2ws=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=88=B0=E5=85=AC=E5=85=B1=E9=A2=91=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 4d400b5..c974d38 100644 --- a/index.js +++ b/index.js @@ -87,16 +87,22 @@ function websocketer(ws, req) { // 游客使用公共账户 uid = 0 let uid = req.session?.account?.uid || 0 + // 当游客连接时, 为其使用访客公共订阅列表 + if (req.session.account) { + // 当用户连接时, 读取其订阅列表 + db('user').findOne({ uid }, function (err, doc) { + if (doc && Array.isArray(doc.fm)) { + doc.fm.forEach(fid => FM.订阅频道(fid, uid)) + } + }) + } else { + // 访客默认订阅的频道列表: 一般是所有公开的频道 + [].forEach(fid => FM.订阅频道(fid, uid)) + } + // 将连接加入到列表 FM.增加会话(uid, ws) - // 当用户连接时, 读取其订阅列表 - db('user').findOne({ uid }, function (err, doc) { - if (doc && Array.isArray(doc.fm)) { - doc.fm.forEach(fid => FM.订阅频道(fid, uid)) - } - }) - // 收到消息时(只有频道消息) ws.on('message', function (msg) { // 可能需要检查权限, 是否可以向指定目标发送, 或者由客户端过滤