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

发布于 2024-11-27 08:28:16 字数 448 浏览 1 评论 0原文

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() 之间有什么区别?

Socket.io's readme contains the following example:

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

What's the difference between socket.broadcast.to() and io.sockets.in()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乞讨 2024-12-04 08:28:16

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

socket.broadcast.to broadcasts to all sockets in the given room, except to the socket on which it was called while io.sockets.in broadcasts to all sockets in the given room.

尘世孤行 2024-12-04 08:28:16

2019 年更新:socket.io 是一个特殊模块,它使用 websockets,然后回退到 http 请求轮询。仅适用于 websockets:对于客户端,请使用本机 websockets;对于 Node.js,请使用 ws 或此库。

简单示例

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

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

socket.emit('hello', msg);

发送给房间“我的房间”中的所有人,包括发件人(如果发件人在房间内) /em>

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

Update 2019: socket.io is a special module which uses websockets and then fallsback to http request polling. For just websockets: for the client use native websockets and for node.js use ws or this library.

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

Send to the sender and noone else

socket.emit('hello', msg);

Send to everyone including the sender(if the sender is in the room) in the room "my room"

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

Send to everyone except the 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, including the 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);
几味少女 2024-12-04 08:28:16

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

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

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

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

Socket.io Rooms

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

    }
}

干杯

Node.js was something I was really interested forawhile and I used it in one of my project to make a multiplayer game.

io.sockets.in().emit() and socket.broadcast.to().emit() are the main two emit methods we use in Socket.io's Rooms (https://github.com/LearnBoost/socket.io/wiki/Rooms) Rooms allow simple partitioning of the connected clients. This allows events to be emitted with to subsets of the connected client list, and gives a simple method of managing them.

They allow us to manage the subsets of the connected client list(which we call rooms) and have the similiar functionalities like the main socket.io functions io.sockets.emit() and socket.broadcast.emit().

Anyway I'll try to give the example codes with the comments to explain. See if it helps;

Socket.io Rooms

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

    }
}

Cheers

-小熊_ 2024-12-04 08:28:16
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');

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

};
自此以后,行同陌路 2024-12-04 08:28:16

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

查看源代码(v1.0.6):

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

In Socket.IO 1.0, .to() and .in() are the same. And others in the room will receive the message. The client sends it won't receive the message.

Check out source code (v1.0.6):

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文