简体   繁体   中英

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. The main page connects to my Node.JS server using socket.io. The socket is stored in window.socket . Then, in the iframe, I access the socket with parent.window.socket . Running console.log(parent.window.socket) within the iframe shows that it is valid.

From within the iframe, I have a few things. First, handlers are registered, for event A . It has a clear output (console.log), so its not that the output code is bad. I also have socket.on('message') to catch all messages. 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.

I have checked:

  • I am using the right WebSocket object in the iframe code.
  • The iframe and 'viewer' page are server from the same domain and server (not a cross-origin issue)
  • 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.

EDIT: Also tested it in parent page - same behavior. It receives the message in socket.on('message') , but socket.on('A') is not fired.

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. Using iframes had nothing to do with it.

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