io.sockets.emit和socket.broadcast.emit有什么区别?广播只是向所有人发出但发送它的套接字吗?
似乎它们可以互换使用:
io.sockets.on('connection', function (socket) { //these should do the same thing io.sockets.emit('this', { receivers: 'everyone'}); socket.broadcast.emit('this', { receivers: 'everyone but socket'}); //emits to everyone but socket socket.emit('this', { receivers: 'socket'}); //emits to socket });
io.sockets.emit 将发送给所有客户
io.sockets.emit
socket.broadcast.emit 会将消息发送给除新建连接以外的所有其他客户端
socket.broadcast.emit
这篇Socket.IO Wiki帖子将帮助所有人阅读以下问题: