本文介绍了io.sockets.emit 和广播有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

io.sockets.emit 和 socket.broadcast.emit 有什么区别?只是广播向所有人发送,而不是发送它的套接字吗?

What's the difference between io.sockets.emit and socket.broadcast.emit? Is it only that broadcast emits to everyone BUT the socket that sends it?

它们似乎可以互换使用:

It seems like they can be used interchangeably:

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 将发送给所有客户端

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

socket.broadcast.emit will send the message to all the other clients except the newly created connection

这篇 Socket.IO Wiki 帖子将帮助每个人阅读这个问题:

最近的备忘单也可以在这里查看:

The recent cheatsheet can also be viewed here:

https://socket.io/docs/v4/emit-cheatsheet

这篇关于io.sockets.emit 和广播有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:09