简体   繁体   English

socket.broadcast.emit() 和 io.to(socketID).emit() 之间有什么区别?

[英]What is the difference between - socket.broadcast.emit() and io.to(socketID).emit()?

Getting started with using socket.io library.开始使用 socket.io 库。 There is this use case where I need to broadcast to all connected clients except the sender.在这个用例中,我需要向除发件人以外的所有连接的客户端广播。 Therefore, I had used the socket.broadcast.emit() .因此,我使用了socket.broadcast.emit()

I came across another technique to achieve the same which was io.to(socketID).emit() .我遇到了另一种实现相同效果的技术io.to(socketID).emit() From what I understand, this method emits to a room with an id = socketID (ideally this room would have just one client by default and the event will be emitted to this client).据我了解,此方法会发送到一个 id = socketID 的房间(理想情况下,这个房间默认只有一个客户端,并且事件将发送到该客户端)。

Is there any other functional difference between these two methods or can I use them interchangeably?这两种方法之间是否有任何其他功能差异,或者我可以互换使用它们吗?

Tried both ways, and they both seemed to work fine.尝试了两种方式,它们似乎都工作正常。 Just wanted to know if there's any difference and any limitations to either way of emitting.只是想知道这两种发射方式是否有任何区别和限制。

io.on("connection", (socket) => {
    setInterval(() => {
        socket.broadcast.emit('dummy_event', 'new_testing_21jan');
    }, 5000);
}

and

io.on("connection", (socket) => {
    setInterval(() => {
        io.to(socket.id).emit('dummy_event', 'new_testing_21jan');
    }, 5000);
}

socket.broadcast.emit() broadcasts an event to all connected clients except the sender. socket.broadcast.emit()向除发送者之外的所有连接的客户端广播一个事件。

io.to(socketID).emit() is used to send event to specific socket by socket ID. io.to(socketID).emit()用于通过套接字ID向特定套接字发送事件。

Both can be used interchangeably in your use case, but io.to(socketID).emit() is more specific and useful for emitting events to specific clients.两者都可以在您的用例中互换使用,但io.to(socketID).emit()对于向特定客户端发出事件更为具体和有用。

socket.broadcast.emit() wants to send the event to all clients except the sender. socket.broadcast.emit()想要将事件发送给除发送者之外的所有客户端。

You can use io.in(roomName).emit() to send an event to all customers in a particular room.您可以使用io.in(roomName).emit()向特定房间中的所有客户发送事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么发件人也收到 socket.broadcast.emit 事件? - Why the sender recieve the socket.broadcast.emit event also? socket.io-发射到socketid(单个客户端)不起作用 - socket.io - emit to socketid(single client) not working socket.io向每个客户端发出/广播 - socket.io emit/broadcast to every client 即使有些客户端未将侦听器设置为“ myMessage”,socket.broadcast.emit('myMessage',data')也会向所有客户端发出消息吗? - Do socket.broadcast.emit('myMessage', data') emit the message to all clients even if some have not set listener to 'myMessage'? AngularJS中的$ rootScope和$ rootScope。$ emit / $ broadcast有什么区别? - What is the difference between $rootScope vs $rootScope.$emit/$broadcast in AngularJS? Socket.io broadcast.to无法正常工作,但broadcast.emit可以 - Socket.io broadcast.to not working but broadcast.emit does Socket.io broadcast.emit 不工作,但它在没有广播的情况下工作 - Socket.io broadcast.emit is not working but it works without broadcast io.emit(...)和loop有什么区别? - What's difference between io.emit(…) and loop? 发送给启动请求socket.io的特定客户端。 在路由器中获取套接字ID - Emit to a particular client who started the request socket.io. Get socketid in router Broadcast.emit不排除Socket.IO中的发送者 - Broadcast.emit not excluding sender in Socket.IO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM