Socket.io在broadcast.to和sockets.in之间的区别

kne*_*nex 99 socket.io

Socket.io的自述文件包含以下示例:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.join('justin bieber fans');
  socket.broadcast.to('justin bieber fans').emit('new fan');
  io.sockets.in('rammstein fans').emit('new non-fan');
});
Run Code Online (Sandbox Code Playgroud)

socket.broadcast.to()和之间有什么区别io.sockets.in()

Dan*_*lig 119

socket.broadcast.to广播到给定房间中的所有套接字,除了io.sockets.in广播到给定房间中的所有套接字时调用它的套接字.

  • 没有.同一事物的不同名称. (6认同)

Gok*_*ank 37

Node.js是我真正感兴趣的东西,我在我的一个项目中使用它来制作多人游戏.

io.sockets.in().emit()并且socket.broadcast.to().emit()是我们在Socket.io的房间中使用的主要两种发射方法(https://github.com/LearnBoost/socket.io/wiki/Rooms)房间允许对连接的客户端进行简单的分区.这允许事件与连接的客户端列表的子集一起发出,并提供一种管理它们的简单方法.

它们允许我们管理连接的客户端列表(我们称之为房间)的子集,并具有类似主要socket.io函数io.sockets.emit()和类似功能socket.broadcast.emit().

无论如何,我会尝试给出带有注释的示例代码来解释.看看它是否有帮助;

Socket.io客房

i)io.sockets.in().emit();

/* Send message to the room1. It broadcasts the data to all 
   the socket clients which are connected to the room1 */

io.sockets.in('room1').emit('function', {foo:bar});
Run Code Online (Sandbox Code Playgroud)

ii)socket.broadcast.to().emit();

io.sockets.on('connection', function (socket) {
    socket.on('function', function(data){

        /* Broadcast to room1 except the sender. In other word, 
            It broadcast all the socket clients which are connected 
            to the room1 except the sender */
        socket.broadcast.to('room1').emit('function', {foo:bar});

    }
}
Run Code Online (Sandbox Code Playgroud)

Socket.io

i)io.sockets.emit();

/* Send message to all. It broadcasts the data to all 
   the socket clients which are connected to the server; */

io.sockets.emit('function', {foo:bar});
Run Code Online (Sandbox Code Playgroud)

ii)socket.broadcast.emit();

io.sockets.on('connection', function (socket) {
    socket.on('function', function(data){

        // Broadcast to all the socket clients except the sender
        socket.broadcast.emit('function', {foo:bar}); 

    }
}
Run Code Online (Sandbox Code Playgroud)

干杯


Kar*_*son 33

更新2017:我建议开始查看本机websockets,因为它们将在以后成为标准.

简单的例子

语法在socketio中令人困惑.此外,每个套接字都会自动连接到他们自己的房间ID socket.id(这是私人聊天在socketio中工作的方式,他们使用房间).

发送给发件人,而不是其他人

socket.emit('hello', msg);
Run Code Online (Sandbox Code Playgroud)

发送给所有人,包括发件人(如果发件人在房间里)在房间"我的房间"

io.to('my room').emit('hello', msg);
Run Code Online (Sandbox Code Playgroud)

房间"我的房间"发送给发件人以外的所有人(如果发件人在房间里)

socket.broadcast.to('my room').emit('hello', msg);
Run Code Online (Sandbox Code Playgroud)

发送给每个房间的每个人,包括发件人

io.emit('hello', msg); // short version

io.sockets.emit('hello', msg);
Run Code Online (Sandbox Code Playgroud)

仅发送到特定套接字(私人聊天)

socket.broadcast.to(otherSocket.id).emit('hello', msg);
Run Code Online (Sandbox Code Playgroud)


小智 7

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 a specific room in a specific namespace, including sender
  io.of('myNamespace').to('room').emit('event', 'message');

  // sending to individual socketid (private message)
  io.to(`${socketId}`).emit('hey', 'I just met you');

  // WARNING: `socket.to(socket.id).emit()` will NOT work, as it will send to everyone in the room
  // named `socket.id` but the sender. Please use the classic `socket.emit()` instead.

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

  // specifying whether the data to send has binary data
  socket.binary(false).emit('what', 'I have no binaries!');

  // sending to all clients on this node (when using multiple nodes)
  io.local.emit('hi', 'my lovely babies');

  // sending to all connected clients
  io.emit('an event sent to all connected clients');

};
Run Code Online (Sandbox Code Playgroud)