简体   繁体   English

会触发Socket.io“消息”事件,但不会触发个别侦听器。 在iframe中注册的监听器

[英]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: 我检查过:

  • I am using the right WebSocket object in the iframe code. 我在iframe代码中使用了正确的WebSocket对象。
  • The iframe and 'viewer' page are server from the same domain and server (not a cross-origin issue) iframe和“查看器”页面是来自同一域和服务器的服务器(不是跨域问题)
  • Messages are received on both ends but server isn't having the custom handler fired. 两端都收到了消息,但是服务器未触发自定义处理程序。
  • No errors in console. 控制台中没有错误。
  • Have tried using firefox debugger to step through code, and the variables and data sent is all correct. 尝试使用firefox调试器单步执行代码,并且发送的变量和数据都正确。

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.

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