[英]Socket.io 'message' event fires, but individual listeners don't fire. Listeners registered in iframe
I have a main page, with an iframe in it. 我有一个主页,其中包含iframe。 The main page connects to my Node.JS server using socket.io.
主页使用socket.io连接到我的Node.JS服务器。 The socket is stored in
window.socket
. 套接字存储在
window.socket
。 Then, in the iframe, I access the socket with parent.window.socket
. 然后,在iframe中,我使用
parent.window.socket
访问套接字。 Running console.log(parent.window.socket)
within the iframe shows that it is valid. 在iframe中运行
console.log(parent.window.socket)
表示它有效。
From within the iframe, I have a few things. 在iframe中,我有几件事。 First, handlers are registered, for event
A
. 首先,为事件
A
注册处理程序。 It has a clear output (console.log), so its not that the output code is bad. 它具有清晰的输出(console.log),因此其输出代码不是错误的。 I also have
socket.on('message')
to catch all messages. 我也有
socket.on('message')
来捕获所有消息。 Finally, I have some emits to send messages to the server. 最后,我有一些发射将消息发送到服务器。
I have confirmed that the server is connecting and receiving the messages, because the events from my emits and the connect/disconnect events fire. 我已经确认服务器正在连接并接收消息,因为来自我的发射事件和连接/断开事件都将触发。 However, when the server sends back a message in response, the event doesn't fire .
但是,当服务器发回一条消息作为响应时,该事件不会触发 。 In
socket.on('message')
, I receive the message fine, with the proper data and arguments, but my event, socket.on('A')
, doesn't fire. 在
socket.on('message')
,我收到的消息很好,带有正确的数据和参数,但是我的事件socket.on('A')
不会触发。
I have checked: 我检查过:
EDIT: Also tested it in parent page - same behavior. 编辑:也在父页面中对其进行了测试-相同的行为。 It receives the message in
socket.on('message')
, but socket.on('A')
is not fired. 它在
socket.on('message')
接收消息,但不会触发socket.on('A')
。
Figured it out. 弄清楚了。 Was using a mix of socket.send() and socket.emit() on client and server, but, according to this post , send only fires the 'message' event and 'emit' fires the custom event.
在客户端和服务器上混合使用了socket.send()和socket.emit(),但是,根据这篇文章 ,send仅触发'message'事件,而'emit'触发自定义事件。 Using iframes had nothing to do with it.
使用iframe与它无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.