io。sockets有什么不同呢?排放和广播吗?

时间:2021-08-21 13:36:23

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?

io。sockets有什么不同呢?排放和socket.broadcast.emit吗?是不是只有广播向每个人发出,而不是发送它的插座?

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

3 个解决方案

#1


141  

io.sockets.emit will send to all the clients

io.sockets。发射将发送给所有的客户端。

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

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

This Socket.IO Wiki post will help everyone reading this question:

这个套接字。IO Wiki文章将帮助大家阅读这个问题:

#2


38  

socket.broadcast.emit() behaves similar to io.sockets.emit, but instead of emitting to all connected sockets it will emit to all connected socket except the one it is being called on. So in this case the socket referenced by socket will not receive the event.

emit .emit()的行为类似于io.sockets。发射,但不是发射到所有连接的套接字,它将发射到所有连接的套接字,除了它正在被调用的那个。因此,在这种情况下,套接字引用的套接字将不会接收事件。

#3


4  

Scenario:1:- By the use of io.sockets.emit Detailed Diagram:-io.sockets.emit

场景:1:-通过使用io.sockets。发出详细图:-io.sockets.emit

Here Every Socket gets the Message including Initiator.

在这里,每个套接字都获得消息,包括启动程序。

  // BY IO>SOCKETS>EMIT
   io.sockets.emit('MyChannelBroadcast',
               {
                 owner:"Anshu Ashish",
                 clientCount:clients,
                 message:"Welcome All"
               }
    );

Scenario:2:- By the use of socket.broadcast.emit Detailed Diagram:-socket.broadcast.emit

场景:2:-通过使用足球。广播。发出详细图:-socket.broadcast.emit

Here Every Sockets are getting Message Except One i.e Initiator.

这里每个套接字都接收消息,除了一个i。启动程序。

    // BY SOCKET>BROADCAST>EMIT
   socket.broadcast.emit('BroadCastExceptMe',{data:"HAVE A NICE DAY"});

Conclusion:- Now it will totally depends our business requirement that which one will be preferable.

结论:-现在完全取决于我们的业务需求,哪个更好。

#1


141  

io.sockets.emit will send to all the clients

io.sockets。发射将发送给所有的客户端。

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

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

This Socket.IO Wiki post will help everyone reading this question:

这个套接字。IO Wiki文章将帮助大家阅读这个问题:

#2


38  

socket.broadcast.emit() behaves similar to io.sockets.emit, but instead of emitting to all connected sockets it will emit to all connected socket except the one it is being called on. So in this case the socket referenced by socket will not receive the event.

emit .emit()的行为类似于io.sockets。发射,但不是发射到所有连接的套接字,它将发射到所有连接的套接字,除了它正在被调用的那个。因此,在这种情况下,套接字引用的套接字将不会接收事件。

#3


4  

Scenario:1:- By the use of io.sockets.emit Detailed Diagram:-io.sockets.emit

场景:1:-通过使用io.sockets。发出详细图:-io.sockets.emit

Here Every Socket gets the Message including Initiator.

在这里,每个套接字都获得消息,包括启动程序。

  // BY IO>SOCKETS>EMIT
   io.sockets.emit('MyChannelBroadcast',
               {
                 owner:"Anshu Ashish",
                 clientCount:clients,
                 message:"Welcome All"
               }
    );

Scenario:2:- By the use of socket.broadcast.emit Detailed Diagram:-socket.broadcast.emit

场景:2:-通过使用足球。广播。发出详细图:-socket.broadcast.emit

Here Every Sockets are getting Message Except One i.e Initiator.

这里每个套接字都接收消息,除了一个i。启动程序。

    // BY SOCKET>BROADCAST>EMIT
   socket.broadcast.emit('BroadCastExceptMe',{data:"HAVE A NICE DAY"});

Conclusion:- Now it will totally depends our business requirement that which one will be preferable.

结论:-现在完全取决于我们的业务需求,哪个更好。