From 4c87d7e55b59f021b3ba0182b55a3937ba18e5f9 Mon Sep 17 00:00:00 2001 From: satori Date: Tue, 14 Dec 2021 06:26:18 +0800 Subject: [PATCH] websocket user --- fmhub.js | 8 ++++---- index.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/fmhub.js b/fmhub.js index 52c32f7..48ce103 100644 --- a/fmhub.js +++ b/fmhub.js @@ -6,7 +6,7 @@ export default class { 订阅频道(fid, uid) { console.log(`用户 ${uid} 订阅了 ${fid}`) let channel = this.channels.get(fid) || new Map() - if (!channel) this.channels.set(fid, channel) + if (!channel.size) this.channels.set(fid, channel) channel.set(uid, true) } 取消订阅(fid, uid) { @@ -19,7 +19,7 @@ export default class { 增加会话(uid, ws) { console.log(`用户 ${uid} 建立了新的会话连接`) let user = this.users.get(uid) || new Map() - if (!user) this.users.set(uid, user) + if (!user.size) this.users.set(uid, user) user.set(ws, true) } 移除会话(uid, ws) { @@ -36,11 +36,11 @@ export default class { console.log("发送消息", fm, uid, data) let msg = JSON.stringify({ fm, uid, data }) let channel = this.channels.get(fm) || new Map() - if (!channel) this.channels.set(fm, channel) + if (!channel.size) this.channels.set(fm, channel) channel.forEach((value, userid) => { console.log(userid, value) let user = this.users.get(userid) - if (!user) return console.log("订阅频道的用户不在线, 应移除此订阅"); + if (!user.size) return console.log("订阅频道的用户不在线, 应移除此订阅"); user.forEach((value, ws) => { ws.send(msg) }) diff --git a/index.js b/index.js index f99bdca..444123e 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,7 @@ function websocketer(ws, req) { // 收到消息时(只有频道消息) ws.on('message', function (msg) { - if (typeof (msg) === 'string') return console.log("消息不是字符串") + if (typeof (msg) !== "string") return console.log("消息不是字符串") let { fm, data } = JSON.parse(msg) FM.发送消息(fm, uid, data) })