问题描述
我有一个带有Redux和persistor的Socket IO和Session的应用程序。 问题在于每个会话的套接字正在侦听当时在我的应用程序中打开的所有会话。
示例: 我在client1上并登录进入该应用程序。我每分钟收到100个请求(根据我的套接字设置)。 如果client2也进入该应用程序,我(client1)和他(client2)将开始每分钟接收200个请求。 如果client3进入,我们三个人每分钟将收到300个请求,依此类推。
也就是说,client1会话套接字正在侦听应用程序中所有打开的会话,并且应该仅侦听来自client1的特定会话中记录的请求。
服务器:
const ioServer = require('http').createServer(server)
class IoConta {
constructor () {
let getNotifications
io.on('connection',socket => {
if (getNotifications) clearInterval(getNotifications)
socket.on('getNotifications',async data => {
getNotifications = setInterval(
() => this.getNotifications(data.id),2000
)
})
io.on('disconnect',() => {
clearInterval(getNotifications)
})
ioServer.listen(3901)
}
async getNotifications (id) {
const novas = await Notificacoes.find({
conta: id,read: false
})
io.emit('notifications',{ notifications: novas,conta: id })
}
前:
const urlIO = 'http://localhost:3901'
const signOut = () => {
useEffect(() => {
const socket = io(urlIO)
socket.emit('getNotifications',{ id: profile.id })
socket.on('notifications',data => {
if (profile.id === data.conta) {
dispatch(somedispatch(data)) }
})
},[])
我需要Socket仅侦听来自登录到特定会话的客户端的请求,也就是说,每个客户端都具有自己的连接,该连接与链接到其ID的套接字有关。
我是在做错事,例如通过在套接字的“连接”内部调用.on
还是其他事?
感谢您的帮助
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)