websocket user
This commit is contained in:
parent
dcfbcdf8ce
commit
4c87d7e55b
8
fmhub.js
8
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)
|
||||
})
|
||||
|
2
index.js
2
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)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user