简体   繁体   中英

Is there a way I can call an event to a specific client rather than all of the connected clients using socket.io?

我不想使用io.emit()向所有连接的客户端发出事件,而是想向一个客户端调用事件。

You have to get the socket object for the particular client you want to send to and use:

someSocket.emit(...)

to just that one socket. You will need to be able to get the appropriate socket for that one specific user. You have not disclosed anything about how you app works for us to know how to recommend doing that. Each socket has an id which can be looked up or you can keep your own index of each socket in your own data structure when they connect and disconnect.

To summarize: io.emit() sends to all connected users. someSocket.emit() sends to just that one socket.


FYI, you don't "call an event to a specific client". You can "send a message to a specific client". Your client code can then register to be notified when that specific incoming message arrives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM