【问题标题】:Socket.io rooms difference between broadcast.to and sockets.inSocket.io 房间广播.to 和 sockets.in 之间的区别
【发布时间】:2011-10-15 23:03:24
【问题描述】:

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

socket.broadcast.to()io.sockets.in() 有什么区别?

【问题讨论】:

标签: node.js socket.io


【解决方案1】:

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

【讨论】:

  • 频道与房间有何不同?
  • 没有。同一事物的不同名称。
  • socket.io 使用术语房间而不是频道。不过,不要将房间/频道与 socket.io 中的命名空间混淆。我更新了我的答案以使用正确的术语。
【解决方案2】:

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()

无论如何我会尝试给出带有cmets的示例代码来解释。看看有没有帮助;

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

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

    }
}

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

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

    }
}

干杯

【讨论】:

    【解决方案3】:

    2019 年更新:socket.io 是一个特殊的模块,它使用 websockets,然后回退到 http 请求轮询。仅适用于 websockets:客户端使用原生 websockets,node.js 使用 ws 或这个库。

    简单示例

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

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

    socket.emit('hello', msg);
    

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

    io.to('my room').emit('hello', msg);
    

    发送给所有人除了发件人(如果发件人在房间里)在房间“我的房间”

    socket.broadcast.to('my room').emit('hello', msg);
    

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

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

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

    socket.broadcast.to(otherSocket.id).emit('hello', msg);
    

    【讨论】:

    • 如何找到 otherSocket.id。在哪里设置?
    • @ImanMarashi 您需要做的就是获取另一个套接字对象,然后访问它的 id 属性。 otherSocket.on('connect',()=> { console.log(otherSocket.id); });
    • 太棒了! io.to('我的房间').emit('你好', msg);它帮助我:)
    • @ImanMarashi 您将 otherSocket.id 保存在外部的数组或对象中。稍后从被调用的任何套接字访问它。
    • 很好的答案!我们将如何使用 redis 作为适配器来完成这些工作?
    【解决方案4】:
    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');
    
    };
    

    【讨论】:

    • 你能提供一个解释伴随代码吗?通常只提供代码是不被接受的。但是,我可以看到您的代码得到了很好的注释:)
    【解决方案5】:

    在 Socket.IO 1.0 中,.to() 和 .in() 是相同的。房间里的其他人将收到该消息。客户端发送它不会收到消息。

    查看源代码(v1.0.6):

    https://github.com/Automattic/socket.io/blob/a40068b5f328fe50a2cd1e54c681be792d89a595/lib/socket.js#L173

    【讨论】:

    • 既然.to(),in 是相同的,那么当我创建一个与某个套接字的ID 完全相同的名称的房间时会发生什么。那么socket.broadcast.to(socketid) 会做什么呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2017-11-06
    • 2015-01-24
    • 2015-09-27
    • 1970-01-01
    • 2020-08-18
    • 2016-07-11
    相关资源
    最近更新 更多