UniWebView message throttling/collision?

381 Views Asked by At

I have the following listener setup in my Unity scene:

ui.OnMessageReceived += (view, message) => {
    var path = message.Path;
    var action = message.Args ["action"];
    if (path == "app") {
        if (action == "log") {
            Debug.Log ("[W] " + message.Args ["text"]);
        }
    }   
};

And in my web view I have this log function:

log: function(m) {
    window.location.href = 'uniwebview://app?action=log&text=' + m;
}

When I execute the following code, the only output that shows up in logcat are tests 5 and E:

app.log("Echo Test (1)");
app.log("Echo Test (2)");
app.log("Echo Test (3)");
app.log("Echo Test (4)");
app.log("Echo Test (5)");
setTimeout(function() {
    app.log("Echo Test (A)");
    app.log("Echo Test (B)");
    app.log("Echo Test (C)");
    app.log("Echo Test (D)");
    app.log("Echo Test (E)");
}, 500);
08-16 13:55:20.229 13860 13881 I Unity   : [W] Echo Test (5)
08-16 13:55:20.693 13860 13881 I Unity   : [W] Echo Test (E)

What is causing this and how can it be fixed?

1

There are 1 best solutions below

0
On BEST ANSWER

Looks like the URL doesn't get time to post to your listener. Your instantly changing the address so it's not executing. You added the timeout which is a sensible thing to do, I suggest you creating a function that waits after each log.

Here is another post having a similar issue that required a time out.

log: function(m) {
  setTimeout(function(){
     window.location.href = 'uniwebview://app?action=log&text=' + m;
  },500 
}

I haven't tested this but if you didn't want to use a timer with 500 milliseconds you could probably check to see if the page has finished loading before you execute the next request

var readyStateCheckInterval = setInterval(function() {
    if (document.readyState === "complete") {
        clearInterval(readyStateCheckInterval);
        init();
    }
}, 10);