Reputation: 894
I'm currently implementing a cross-plattform mobile app with Phonegap, using HTML5 and Javascript. I decided to use Websockets to establish a connection to our backend server for communication and exchanging data. Websockets are supported by Blackberry and iOs browsers by default. For Android there are some promising plugins to integrate websockets into Android java code and execute it in Phonegap using Javascript. I chose this one:
I integrated that plugin into my android project and followed every step in the readme. Then I tested it on physical device Samsung Galaxy Nexus with Android 4.0.3. It worked fine and there where no visible differences to Blackberry and iOs usage. I also deployed the app to a Samsung Galaxy Tab 2
Well, my problem occurs if I deploy the app to an Android device with os version higher than 4.0.3 (testet on 4.1.2 and 4.2.2). It even appears a problem if I use a Samsung Galaxy Nexus with Android 4.2.2!
The juicy thing is, that I use Phonegap 2.6.0 which automatically creates Android project which is based on OS version 4.2.2!!
The Problem is, that the Websocket server inside the native Android part of the project cannot open the connection to the requesting client. Normally the client sends a request to open connection and the server notify the client via callback that the connection has been established.
If Android 4.0.4+ is used the server connection returns a 404 BAD REQUEST message instead of opening the connection.
Due to that issue I tried some other plugins, in fact every single one I could found. All of them worked fine on Android 4.0.3, none on higher versions.
Has anyone any experience in that specific topic and can give me a hint, what I possibly forgot or which points i have to consider? I cannot imagine that no working websocket plugin for Android 4.0.4+ are available!
Upvotes: 1
Views: 2565
Reputation:
I also faced a similar issue and this could be fixed in two ways:
If there is a fallback possible to long-polling, It will work fine. Just try this out. Not recommended from my side as it will be technology downgrade.
There is a buggy implementation of webSockets on Samsung and LG devices having Android version 4.1 and 4.2. This is tested from my end and if you can check on Google devices(Nexus) with the same version, it should work. In order to remove this bug, you need to put a timeout, while connecting via webSocket.
You can refer the below links for reference:
link 1: https://github.com/zotonic/zotonic/issues/569 Link 2:https://github.com/Modernizr/Modernizr/issues/1399
Upvotes: 0
Reputation: 22011
There might be 2 issues here: the library wasn't updated for long and only implements an outdated version of the WebSocket protocol. Second, recent Android versions don't allow any networking activity on the main/UI thread.
AutobahnAndroid implements RFC6455 WebSocket and performs all network activity on background threads. There isn't any integration with Phonegap though.
Disclaimer: I am original author of Autobahn and work for Tavendo.
Upvotes: 1