websocket user

This commit is contained in:
satori 2021-12-14 06:26:18 +08:00
parent dcfbcdf8ce
commit 4c87d7e55b
2 changed files with 5 additions and 5 deletions

View File

@ -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)
})

View File

@ -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)
})