socket.io客户端到客户端消息传递 [英] socket.io client to client messaging
问题描述
我无法使用socket.io获取基本客户端到客户端(或真正的客户端 - >服务器 - >客户端)。下面是我现在的代码:
I'm having trouble getting basic client to client (or really client->server->client) working with socket.io. Heres the code I have right now:
io.sockets.on('connection', function (socket) {
users.push(socket.sessionId);
for(userID in users) {
console.log(userID);
io.sockets.socket(userID).emit('message', { msg: 'New User Connected succesfully' });
}
socket.emit('message', { msg: 'Connected succesfully' });
socket.on('my other event', function (data) {
console.log(data);
});
});
根据我的理解,这应该将新用户消息发送给每个连接的用户(单独,因为我想要以后做实际的个人信息)。相反,我最后只得到连接成功的消息。我的服务器或客户端没有任何错误或其他负面指示。
From my understanding, that should send the new user message to every connected user (individually, since i want to do actual individual messages later). Instead, I only get the 'connected successfully' message at the end. I don't get any errors or other negative indicators from my server or client.
为什么 io.sockets.socket(userID)的任何想法.emit()
无法使用或在其位置使用什么?
Any ideas of why io.sockets.socket(userID).emit()
doesn't work or what to use in its place?
推荐答案
尝试
users.push(socket); // without .sessionId
for (var u in users) {
// users[u] is now the socket
console.log(users[u].id);
users[u].emit('message', { msg: 'New User Connected succesfully' });
}
这篇关于socket.io客户端到客户端消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!