node.js socket.io 仅向发送方发送数据包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24100218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.io send packet to sender only
提问by Zander17
I have yet to figure out how to directly respond to only the sender using socket.io
我还没有弄清楚如何使用 socket.io 直接响应发件人
I have learned that io.sockets.emit sends to all clients but I wont to send information back to the sender.
我了解到 io.sockets.emit 会发送给所有客户端,但我不会将信息发送回发件人。
code:
代码:
socket.on('login', function (data) {
db.users.find({username: cc.lowerCase(data.username)}, function(err, users) {
if (users.length > 0) {
users.forEach( function(user) {
console.log(user.length);
if (user.password == data.password) {
io.sockets.emit('login', { username: user.username });
} else {
io.sockets.emit('error', { message: "Wrong username or password!" });
}
});
} else {
io.sockets.emit('error', { message: "Wrong username or password!" });
}
});
});
回答by Denys Séguret
When your server listens, you usually get a socket at the "connection" event :
当您的服务器侦听时,您通常会在“连接”事件中获得一个套接字:
require('socket.io').on('connect', function(socket){
A socket connects 2 points : the client and the server. When you emit on this socket, you emit to this specific client.
一个套接字连接两个点:客户端和服务器。当你在这个套接字上发射时,你发射到这个特定的客户端。
Example :
例子 :
var io = require('socket.io');
io.on('connect', function(socket){
socket.on('A', function(something){
// we just received a message
// let's respond to *that* client :
socket.emit('B', somethingElse);
});
});
Be careful that those are two different calls :
请注意,这是两个不同的调用:
socket.emit: emit to just one socketio.sockets.emit: emit to all sockets
socket.emit: 只发射到一个套接字io.sockets.emit: 发射到所有套接字
回答by Karl Morrison
Simple example
简单的例子
The syntax is confusing in socketio. Also, every socket is automatically connected to their own room with the id socket.id(this is how private chat works in socketio, they use rooms).
socketio 中的语法令人困惑。此外,每个套接字都会使用 id 自动连接到他们自己的房间socket.id(这是 socketio 中私人聊天的工作方式,他们使用房间)。
Send to the sender and noone else
发送给发件人而不是其他人
socket.emit('hello', msg);
Send to everyone includingthe sender(if the sender is in the room) in the room "my room"
发送给房间“我的房间”中的所有人,包括发件人(如果发件人在房间内)
io.to('my room').emit('hello', msg);
Send to everyone exceptthe sender(if the sender is in the room) in the room "my room"
发送给房间“我的房间”中除发件人之外的所有人(如果发件人在房间内)
socket.broadcast.to('my room').emit('hello', msg);
Send to everyone in every room, includingthe sender
发送给每个房间的每个人,包括发件人
io.emit('hello', msg); // short version
io.sockets.emit('hello', msg);
Send to specific socket only (private chat)
仅发送到特定套接字(私人聊天)
socket.broadcast.to(otherSocket.id).emit('hello', msg);
回答by Armando Vrushi
late but better than never ;)
迟到总比没有好 ;)
I had smth like this:
我有这样的经历:
io.on('connection', function(socket){
socket.emit('some event', data);
socket.on('private event', function(message){
this.emit('other private event', message);
}
}
After some testing, I realized that 'this.emit' inside 'private event' closure send back only to sender. So i tried 'this.broadcast.emit', which send the message to all connected except the sender. I believe this is what you want from your code.
经过一些测试,我意识到 'private event' 闭包中的 'this.emit' 只发送回发件人。所以我尝试了'this.broadcast.emit',它将消息发送到除发件人之外的所有连接。我相信这就是您想要的代码。

