在Socket.io 1.0和Socket.io 0.9中,将一条消息广播到一个房间内所有连接的套接字,以下哪种语法是正确的?
以下都没有向我的客户端Web控制台发送消息,该控制台有一个简单的日志声明,但至少有四个正在发送给其他客户端。
如何正确地将“joined_channel”消息发送回我的已连接客户端以及会议室中的所有其他客户端?
Client
socket.on('joined_channel', function(data){
console.log('joined channel', data);
})
Server
// this is for 0.9
io.sockets.in(data.room).emit('joined_channel', data.room);
// which version is this for? Any?
socket.broadcast.to(data.room).emit('joined_channel', data.room);
// is this to all connected users?
io.sockets.emit('joined_channel', data.room);
// To all connected users globally?
io.emit('joined_channel', data.room);
// how is socket different than io?
socket.emit('joined_channel', data.room);
// this is for 0.9 but does it work at all?
io.sockets.to(data.room).emit('joined_channel', data.room);
// this is as above but 1.0?
io.to(data.room).emit('joined_channel', data.room);
答案 0 :(得分:1)
这是最新的socket.io。
取自http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/
//emit from server to everyone but the clients socket
socket.broadcast.emit('joined_channel', data);
//broadcast information to a certain room only (excluding the client)
socket.broadcast.to( data.room ).emit('joined_channel');
//broadcast information globally to a certain room
io.sockets.in( data.room ).emit('joined_channel');