当用户的套接字 id 直接存储在 io.sockets.on('connect') 函数中时,我似乎只能向用户发送消息。我不知道为什么在他们登录后尝试存储他们的套接字 id 时它不起作用。
工作:
var clients = {};
/** A new socket connection has been accepted */
io.sockets.on('connection', function (socket)
{
//Used to access session id
var hs = socket.handshake;
clients[socket.id] = socket; // add the client data to the hash
users['brownj2'] = socket.id; // connected user with its socket.id
socket.on('user-login', function(user){
if(!users[username]){
//users[username] = socket.id; // connected user with its socket.id
}
})
socket.on('page-load', function(pid)
{
if(users['brownj2'])
{
clients[users['brownj2']].emit('hello');
}
else
{
console.log("NOT FOUND BROWNJ2");
}
}
}
不工作:
var clients = {};
/** A new socket connection has been accepted */
io.sockets.on('connection', function (socket)
{
//Used to access session id
var hs = socket.handshake;
clients[socket.id] = socket; // add the client data to the hash
socket.on('user-login', function(user){
if(!users['brownj2']){
users['brownj2'] = socket.id; // connected user with its socket.id
}
})
socket.on('page-load', function(pid)
{
if(users['brownj2'])
{
clients[users['brownj2']].emit('hello');
}
else
{
console.log("NOT FOUND BROWNJ2");
}
}
}
JavaScript 客户端代码片段
var socket = io.connect('');
socket.on("hello", function(){
alert("Hi Jack");
console.log("WEBSOCKET RECIEVED");
})
解决方案:感谢@alessioalex 我不得不从登录页面中删除对 socket.io 的引用,并将以下内容添加到 io.sockets.on('connection')
var hs = socket.handshake;
sessionStore.get(hs.sessionID, function(err, session){
console.log("SESSION: " + util.inspect(session));
clients[socket.id] = socket; // add the client data to the hash
validUsers[session.username] = socket.id; // connected user with its socket.id
})
最佳答案
您的代码存在一些问题,首先是您不应该通过 Socket.IO 对用户进行身份验证,您应该确保他们只有在经过身份验证后才能连接。如果您使用 Express,那么以下文章可以帮助您很多:http://www.danielbaulig.de/socket-ioexpress/
此外,您应该避免像这样发送消息,因为这是 Socket.IO 内部的一部分并且可能会发生变化:
io.sockets.socket(id).emit('hello');
相反(如果您想向特定客户端发送消息)最好保留一个对象,例如与连接的客户端(并在断开连接后删除客户端):
// the clients hash stores the sockets
// the users hash stores the username of the connected user and its socket.id
io.sockets.on('connection', function (socket) {
// get the handshake and the session object
var hs = socket.handshake;
users[hs.session.username] = socket.id; // connected user with its socket.id
clients[socket.id] = socket; // add the client data to the hash
...
socket.on('disconnect', function () {
delete clients[socket.id]; // remove the client from the array
delete users[hs.session.username]; // remove connected user & socket.id
});
}
// we want at some point to send a message to user 'alex'
if (users['alex']) {
// we get the socket.id for the user alex
// and with that we can sent him a message using his socket (stored in clients)
clients[users['alex']].emit("Hello Alex, how've you been");
}
当然,io.sockets.socket(id)
可能有效(实际上并未测试),但它也可以随时更改,因为它是 Socket.IO 内部结构的一部分,所以我的解决方案以上更“安全”。
您可能希望在客户端代码中更改的另一件事是 var socket = io.connect('');
和 var socket = io.connect('http://localhost');
,正如我们在Socket.IO的官方示例中看到的:http://socket.io/#how-to-use
关于javascript - 通过 socket.id 向客户端发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8467784/