Socket.io与broadcast.to和sockets.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()什么io.sockets.in() ? 
  socket.broadcast.to广播到给定房间内的所有套接字, 除了被调用的套接字,而io.sockets.in广播到给定房间中的所有套接字。 
Node.js是我真正感兴趣的东西,我用它在我的项目之一,以制作一个多人游戏。
  io.sockets.in().emit()和socket.broadcast.to().emit()是我们在Socket.io的房间中使用的主要的两个发射方法( https://github.com/LearnBoost/socket.io /维客/房间 )房间允许简单分区连接的客户端。 这允许事件被发送到连接的客户端列表的子集,并给出一个简单的pipe理方法。 
 它们允许我们pipe理连接客户端列表(我们称之为房间)的子集,并具有类似主要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}); 
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}); } } 
干杯
更新2017年 :我build议开始寻找本地websockets,因为他们将成为标准。
简单的例子
  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); 
在Socket.IO 1.0中,.to()和.in()是相同的。 房间里的其他人会收到这个消息。 客户端发送它不会收到消息。
查看源代码(v1.0.6):