Socket.io 房间广播.to 和 Socket.in 的区别

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

76685 次浏览

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

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

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

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

无论如何,我将尝试给出示例代码和注释来解释

Socket.io Rooms

I) io.sockets.in ()

/* 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

/* 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)套接字、广播、发射() ;

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


}
}

干杯

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

查看源代码(v1.0.6) :

Https://github.com/automattic/socket.io/blob/a40068b5f328fe50a2cd1e54c681be792d89a595/lib/socket.js#l173

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

举个简单的例子

在套接字中语法是混乱的。此外,每个套接字都自动连接到自己的房间与 id socket.id(这是如何在套接字私人聊天工作,他们使用的房间)。

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

socket.emit('hello', msg);

Send to everyone 包括 the sender(if the sender is in the room) in 房间“我的房间”

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


};