发送到套接字IO socket.id [英] Emit to Socket IO socket.id
本文介绍了发送到套接字IO socket.id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试向特定的套接字ID发出:
I am trying to emit to a particular socket ID:
socket(user[playID]).emit('correct', data);
但我得到:
TypeError: object is not a function
如果我退出 user [playID]
我确实获得了有效的套接字ID。
if I log out user[playID]
I do get a valid socket ID.
赞赏!
这是我的设置,以防我遗漏了什么:
Here is my setup in case I'm missing something:.
// Tell Socket.io to pay attention
servio = io.listen(server);
// Tell HTTP Server to begin listening for connections on port 3250
sock = server.listen(3250);
推荐答案
这应该这样做
servio.sockets.socket(id).emit('hello');
这答案涵盖相同/相似的主题。简而言之,考虑自己保留对连接客户端的引用并根据需要向它们发出,而不是依赖于socket.io的内部结构,这可能会改变。
This answer covers the same/similar topic. In short, consider keeping a reference to the connected clients yourself and emit to them as desired, rather than relying on socket.io's internals, which could change.
这篇关于发送到套接字IO socket.id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文