一尘不染

io.sockets.emit和广播之间有什么区别?

node.js

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

阅读 436

收藏
2020-07-07

共1个答案

一尘不染

io.sockets.emit 将发送给所有客户

socket.broadcast.emit 会将消息发送给除新建连接以外的所有其他客户端

这篇Socket.IO
Wiki帖子将帮助所有人阅读以下问题:

2020-07-07