Search code examples
androidxmlhttprequestsocket.ioandroid-webviewlong-polling

Android Webview: xhr-polling keeps page 'loading'


I have a WebView running a page containing a socket.io client that connects to a remote node.js server. The problem is that because websockets are not supported in the Android webview (they are in the Android Chrome browser), it falls back to xhr-polling.

I have an onProgressChanged listener for the WebChromeClient, but the xhr-polling keeps the page continuously loading. When I log the progress, it reaches ~90-95% and gets no further. This means that I cannot dismiss any loading indicators or perform any other actions that require the page to be fully loaded.

Is there anyway I can make Android think that the xhr-polling is not part of the page loading? Or is there a workaround? I haven't tested it with jsonp-polling yet. I would prefer a solution to this issue, rather than a hack/workaround, but ultimately I just want my app to work.

P.S, I am aware of native libraries for socket.io and WebSockets on android. However, this all needs to be done in a WebView.

Thanks


Solution

  • I found the problem!

    On the socket.io-client apparently there's a special defer if it's XHR + webkit browser in here:

    https://github.com/LearnBoost/socket.io-client/blob/0.9/lib/util.js#L203

    As you can see, 100ms it's not enough for a mobile phone, I had to change it for at least 500ms! I think the authors didn't remember this use case! Patch the socket.io-client with this and profit :)