Node.js 多个套接字客户端断开连接[socket.io]
假设..我有两个连接的客户端 因此,只要我刷新任何一个浏览器,两个客户端都会断开连接 我已经编写了代码,在用户断开连接时移除套接字Node.js 多个套接字客户端断开连接[socket.io],node.js,angular,sockets,chat,disconnect,Node.js,Angular,Sockets,Chat,Disconnect,假设..我有两个连接的客户端 因此,只要我刷新任何一个浏览器,两个客户端都会断开连接 我已经编写了代码,在用户断开连接时移除套接字 let userId = socket.request._query['userId']; socket.on('disconnect', function(){ console.log("disconnect: ", socket.id); User.remove
let userId = socket.request._query['userId'];
socket.on('disconnect', function(){
console.log("disconnect: ", socket.id);
User.removeSocketId(userId, (err, res) => {
if(err) throw err;
//updated code and found this emit was causing the issue, without this code everything work fine.
socket.broadcast.emit('chat-list-response',{
error : false ,
userDisconnected : true ,
socketId : socket.id
});
});
});
因此,由于这个套接字,两个客户端的ID都被删除了
有没有办法阻止多次断开连接
提前谢谢。您可以发布
用户.removeSocketId
函数源代码吗?我需要查看更多代码。我想发生的是变量userId总是对最后一个连接的用户的引用。您需要找到一种更好的方法来保存userId值。我可以建议socket.userId=socket.requrest.\u查询['userId']。然后可以调用User.removeSocketId(socket.userId,(err,res).@gnuns module.exports.removeSocketId=function(idObj,回调){User.findByIdAndUpdate(idObj,{$set:{socketID:,onlineStatus:“N”},回调);}@magreenberg,谢谢你的建议。我会检查一下。找到了导致问题的代码。我已经更新了上面的代码,现在因为我需要发出事件,我必须获得断开连接的客户端的套接字ID并发送给所有其他客户端…无论如何,谢谢大家。