Permalink
Browse files

Avoid "You are trying to call recursively into the Flash Player which…

… is not allowed. In most cases the JavaScript setTimeout function, can be used as a workaround" which will occur if we are sending data and recieved a message at the same time.
  • Loading branch information...
1 parent 6493b0c commit 47e8b7a28ab90a08dc7c4ff98140e7ed4b5c3e82 @jvshahid committed Jan 7, 2011
Showing with 21 additions and 17 deletions.
  1. +21 −17 web_socket.js
View
@@ -205,24 +205,28 @@
// Gets data using readSocketData() instead of getting it from event object
// of Flash event. This is to make sure to keep message order.
// It seems sometimes Flash events don't arrive in the same order as they are sent.
- var arr = this.__flash.readSocketData();
- for (var i = 0; i < arr.length; i++) {
- var data = decodeURIComponent(arr[i]);
- try {
- if (this.onmessage) {
- var e;
- if (window.MessageEvent && !window.opera) {
- e = document.createEvent("MessageEvent");
- e.initMessageEvent("message", false, false, data, null, null, window, null);
- } else { // IE and Opera, the latter one truncates the data parameter after any 0x00 bytes
- e = {data: data};
+ var socket = this.__flash;
+ var onmessage = this.onmessage;
+ setTimeout(function() {
+ var arr = socket.readSocketData();
+ for (var i = 0; i < arr.length; i++) {
+ var data = decodeURIComponent(arr[i]);
+ try {
+ if (onmessage) {
+ var e;
+ if (window.MessageEvent && !window.opera) {
+ e = document.createEvent("MessageEvent");
+ e.initMessageEvent("message", false, false, data, null, null, window, null);
+ } else { // IE and Opera, the latter one truncates the data parameter after any 0x00 bytes
+ e = {data: data};
+ }
+ onmessage(e);
+ }
+ } catch (e) {
+ console.error(e.toString());
+ }
}
- this.onmessage(e);
- }
- } catch (e) {
- console.error(e.toString());
- }
- }
+ }, 0);
};
/**

3 comments on commit 47e8b7a

Just confirmation: by "when we are sending messages and receiving at the same time", you mean something like this?:
ws.onmessage = function() { ws.send(...); };

Owner

jvshahid replied Jan 8, 2011

Honestly I don't know the exact circumstances under which this error happens. Let me explain my use case to put some context to the conversation. In our code, we add messages to a buffer while the socket is connection. When we get the connect event we loop over the buffered messages and send them one at a time. Also, our message protocol returns a reply for every one of these messages. When the connect event is received I'll get a lot of the "You are trying to call recursively..." error message (hundreds of them).

I'm not sure of this, but my explanation is that while we are inside flash after a call to ws.send, the WebSocket class (in WebSocket.as) onSocketData gets called, jumps in the Javascript land and we call readSocketData which causes us to recursively call FABridge (remember we are still sending data).

I verified that this is the case by removing readSocketData which caused the errors to disappear (of course we weren't receiving data).

I see, it may be the case.

I made change similar to your patch, but with some larger cleanup, so I didn't directly pull your change. Thanks anyway.
gimite/web-socket-js@20f8374

This is somewhat large change so it may break something. Please let me know if the specific issue is not solved, or you find other issue, with the change.

Please sign in to comment.