kana/fmhub.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-01-30 17:28:09 +08:00
import interrelated from 'interrelated'
2021-12-14 03:49:35 +08:00
export default class {
constructor() {
2022-01-30 17:28:09 +08:00
this.用户订阅 = new interrelated()
this.用户会话 = new interrelated()
2021-12-14 03:49:35 +08:00
}
订阅频道(fid, uid) {
2022-01-30 17:28:09 +08:00
this.用户订阅.set(fid, uid)
2021-12-14 03:49:35 +08:00
}
取消订阅(fid, uid) {
2022-01-30 17:28:09 +08:00
this.用户订阅.delete(fid, uid)
2021-12-14 03:49:35 +08:00
}
增加会话(uid, ws) {
2022-01-30 17:28:09 +08:00
this.用户会话.set(uid, ws)
2021-12-14 03:49:35 +08:00
}
移除会话(uid, ws) {
2022-01-30 17:28:09 +08:00
this.用户会话.delete(uid, ws)
2021-12-14 03:49:35 +08:00
}
发送消息(fm, uid, data) {
let msg = JSON.stringify({ fm, uid, data })
2022-01-30 17:28:09 +08:00
this.用户订阅.atob(fm, (uid) => {
//console.log(`用户 ${uid} 订阅的所有频道`)
this.用户会话.atob(uid, (ws) => {
//console.log(`用户 ${uid} 的会话`)
2021-12-14 03:49:35 +08:00
ws.send(msg)
})
})
2022-01-30 17:28:09 +08:00
//console.log(`用户 ${uid} 订阅的所有频道`)
//this.用户订阅.aall(uid, (fid) => {
// console.log(fid)
//})
//console.log(`频道 ${fm} 下的所有用户`)
//this.用户订阅.ball(fm, (uid) => {
// console.log(uid)
//})
2021-12-14 03:49:35 +08:00
}
移除用户(uid) {
2022-01-30 17:28:09 +08:00
this.用户订阅.adelete(uid)
this.用户会话.adelete(uid)
2021-12-14 03:49:35 +08:00
}
}