Javascript 向除发送者以外的所有客户端发送响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10058226/
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
Send response to all clients except sender
提问by switz
To send something to all clients, you use:
要将某些内容发送给所有客户端,请使用:
io.sockets.emit('response', data);
To receive from clients, you use:
要从客户那里接收,您可以使用:
socket.on('cursor', function(data) {
...
});
How can I combine the two so that when recieving a message on the server from a client, I send that message to all users except the one sending the message?
如何将两者结合起来,以便在服务器上从客户端接收消息时,将该消息发送给除发送消息的用户之外的所有用户?
socket.on('cursor', function(data) {
io.sockets.emit('response', data);
});
Do I have to hack it around by sending the client-id with the message and then checking on the client-side or is there an easier way?
我是否必须通过发送带有消息的客户端 ID 然后在客户端检查来破解它,还是有更简单的方法?
回答by LearnRPG
Here is my list (updated for 1.0):
这是我的列表(更新为 1.0):
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
// list socketid
for (var socketid in io.sockets.sockets) {}
OR
Object.keys(io.sockets.sockets).forEach((socketid) => {});
回答by soyuka
From the @LearnRPG answer but with 1.0:
来自@LearnRPG 的答案,但使用 1.0:
// send to current request socket client
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.sockets.emit('message', "this is a test"); //still works
//or
io.emit('message', 'this is a test');
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
// docs says "simply use to or in when broadcasting or emitting"
io.in('game').emit('message', 'cool game');
// sending to individual socketid, socketid is like a room
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
To answer @Crashalot comment, socketid
comes from:
回答@Crashalot 评论,socketid
来自:
var io = require('socket.io')(server);
io.on('connection', function(socket) { console.log(socket.id); })
回答by Vadorequest
Here is a more complete answer about what has changed from 0.9.x to 1.x.
这是关于从 0.9.x 到 1.x 的变化的更完整的答案。
// send to current request socket client
socket.emit('message', "this is a test");// Hasn't changed
// sending to all clients, include sender
io.sockets.emit('message', "this is a test"); // Old way, still compatible
io.emit('message', 'this is a test');// New way, works only in 1.x
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");// Hasn't changed
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');// Hasn't changed
// sending to all clients in 'game' room(channel), include sender
io.sockets.in('game').emit('message', 'cool game');// Old way, DOES NOT WORK ANYMORE
io.in('game').emit('message', 'cool game');// New way
io.to('game').emit('message', 'cool game');// New way, "in" or "to" are the exact same: "And then simply use to or in (they are the same) when broadcasting or emitting:" from http://socket.io/docs/rooms-and-namespaces/
// sending to individual socketid, socketid is like a room
io.sockets.socket(socketid).emit('message', 'for your eyes only');// Old way, DOES NOT WORK ANYMORE
socket.broadcast.to(socketid).emit('message', 'for your eyes only');// New way
I wanted to edit the post of @soyuka but my edit was rejected by peer-review.
我想编辑@soyuka 的帖子,但我的编辑被同行评审拒绝了。
回答by Khaled Jouda
broadcast.emit sends the msg to all other clients (except for the sender)
broadcast.emit 将 msg 发送到所有其他客户端(发送者除外)
socket.on('cursor', function(data) {
socket.broadcast.emit('msg', data);
});
回答by Prasanna Brabourame
Emit cheatsheet
发出备忘单
io.on('connect', onConnect);
function onConnect(socket){
// sending to the client
socket.emit('hello', 'can you hear me?', 1, 2, 'abc');
// sending to all clients except sender
socket.broadcast.emit('broadcast', 'hello friends!');
// sending to all clients in 'game' room except sender
socket.to('game').emit('nice game', "let's play a game");
// sending to all clients in 'game1' and/or in 'game2' room, except sender
socket.to('game1').to('game2').emit('nice game', "let's play a game (too)");
// sending to all clients in 'game' room, including sender
io.in('game').emit('big-announcement', 'the game will start soon');
// sending to all clients in namespace 'myNamespace', including sender
io.of('myNamespace').emit('bigger-announcement', 'the tournament will start soon');
// sending to individual socketid (private message)
socket.to(<socketid>).emit('hey', 'I just met you');
// sending with acknowledgement
socket.emit('question', 'do you think so?', function (answer) {});
// sending without compression
socket.compress(false).emit('uncompressed', "that's rough");
// sending a message that might be dropped if the client is not ready to receive messages
socket.volatile.emit('maybe', 'do you really need it?');
// sending to all clients on this node (when using multiple nodes)
io.local.emit('hi', 'my lovely babies');
};
回答by runspired
For namespaces within rooms looping the list of clients in a room (similar to Nav's answer) is one of only two approaches I've found that will work. The other is to use exclude. E.G.
对于房间内的命名空间,循环房间中的客户端列表(类似于 Nav 的答案)是我发现的仅有的两种可行方法之一。另一种是使用排除。例如
socket.on('message',function(data) {
io.of( 'namespace' ).in( data.roomID ).except( socket.id ).emit('message',data);
}
回答by Kent Aguilar
Updated the list for further documentation.
更新了列表以获取更多文档。
socket.emit('message', "this is a test"); //sending to sender-client only
socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender
socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender
socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel)
socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid
io.emit('message', "this is a test"); //sending to all clients, include sender
io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender
io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender
socket.emit(); //send to all connected clients
socket.broadcast.emit(); //send to all connected clients except the one that sent the message
socket.on(); //event listener, can be called on client to execute on server
io.sockets.socket(); //for emiting to specific clients
io.sockets.emit(); //send to all connected clients (same as socket.emit)
io.sockets.on() ; //initial connection from a client.
Hope this helps.
希望这可以帮助。
回答by Kim Thien Dung
Other cases
其他情况
io.of('/chat').on('connection', function (socket) {
//sending to all clients in 'room' and you
io.of('/chat').in('room').emit('message', "data");
};
回答by reabow
I am using namespaces and rooms - I found
我正在使用命名空间和房间 - 我发现
socket.broadcast.to('room1').emit('event', 'hi');
to work where
去哪里工作
namespace.broadcast.to('room1').emit('event', 'hi');
did not
没有
(should anyone else face that problem)
(如果其他人遇到这个问题)
回答by Abi
use this coding
使用这种编码
io.sockets.on('connection', function (socket) {
socket.on('mousemove', function (data) {
socket.broadcast.emit('moving', data);
});
this socket.broadcast.emit() will emit everthing in the function except to the server which is emitting
此 socket.broadcast.emit() 将发出函数中的所有内容,除了正在发出的服务器