向发件人以外的所有客户发送回复

swi*_*itz 213 javascript node.js socket.io

要向所有客户发送内容,请使用:

io.sockets.emit('response', data);
Run Code Online (Sandbox Code Playgroud)

要从客户处接收,您使用:

socket.on('cursor', function(data) {
  ...
});
Run Code Online (Sandbox Code Playgroud)

如何将两者结合起来,以便在从客户端收到服务器上的消息时,我将该消息发送给除发送消息的用户以外的所有用户?

socket.on('cursor', function(data) {
  io.sockets.emit('response', data);
});
Run Code Online (Sandbox Code Playgroud)

我是否必须通过发送带有消息的client-id然后在客户端检查或者是否有更简单的方法来破解它?

Lea*_*RPG 805

这是我的列表(更新为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) => {});
Run Code Online (Sandbox Code Playgroud)

  • 这比socket.io文档中的所有内容更有用 (106认同)
  • 您是否愿意将此贡献给[FAQ](https://github.com/LearnBoost/socket.io/wiki)?或者我可以帮你吗?(我在这里提供反向链接) (14认同)
  • 你可以根据[socket#in](http://socket.io/docs/server-api/#socket#in(room:string):socket),`socket.to('others')添加它.发出('一个事件',{some:'data'});`也**在一个房间里播放**. (4认同)
  • 作为"//发送给除发件人之外的所有客户端"的补充,用于"//仅向发送方客户端发送响应"的内容? (2认同)

soy*_*uka 42

来自@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');
Run Code Online (Sandbox Code Playgroud)

回答@Crashalot评论,socketid来自:

var io = require('socket.io')(server);
io.on('connection', function(socket) { console.log(socket.id); })
Run Code Online (Sandbox Code Playgroud)

  • 真棒!你怎么得到'socketid`发送到一个单独的插座? (3认同)
  • 编辑回答你的问题.基本上它是来自套接字对象的`socket.id`. (2认同)

Vad*_*est 12

这是一个更完整的答案,关于从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
Run Code Online (Sandbox Code Playgroud)

我想编辑@soyuka的帖子但我的编辑被同行评审拒绝了.


Kha*_*uda 6

broadcast.emit将msg发送给所有其他客户端(发件人除外)

socket.on('cursor', function(data) {
  socket.broadcast.emit('msg', data);
});
Run Code Online (Sandbox Code Playgroud)


Pra*_*ame 5

散发备忘单

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');

};
Run Code Online (Sandbox Code Playgroud)