Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket in Android WebView

today my problem is that I am developing an app that is using a WebSocket. On iOS everything is working out fine but on android the WebView doesn't support the WebSocket. Whats the fastest/most elegant way to solve this problem?

[edit]

As this post is viewed quite often I want to share some possible solutions.

Solutions:

  1. Java Websocket Client Implementation & JavaScript Interface
  2. PHP Websocket Client Implementation & making a background (i.e. jQuery GET) request to the PHP-script (worked for me as I wanted to send only one line of message.)

Android Websocket Libraries

  1. http://autobahn.ws/
  2. http://code.google.com/p/weberknecht

Someone had the same issue and descriped how he worked around

  1. http://chpeck.tumblr.com/post/37273036645/socket-io-with-android-webview
like image 661
Langusten Gustel Avatar asked Nov 22 '12 07:11

Langusten Gustel


3 Answers

I created a sample project which adds the WebSocket object to the WebView for phonegap. You can find it here: https://github.com/pusher/pusher-phonegap-android

I put up a video going over the project here: http://blog.pusher.com/2012/7/5/pusher-on-phonegap-for-android

I used this project to demonstrate how to use PhoneGap with Pusher on Android but ultimately it's just a project that happens to include the Pusher script tag. You could easily use another library that needs the WebSocket object or just use it directly.

It uses the websocket-android-phonegap library: https://github.com/anismiles/websocket-android-phonegap

like image 199
leggetter Avatar answered Oct 30 '22 09:10

leggetter


Android webview doesn't accommodate WebSocket. So you need to implement with some WebSocket library.

jWebSocket http://jwebsocket.org/

weberknecht http://code.google.com/p/weberknecht/

like image 2
wf9a5m75 Avatar answered Oct 30 '22 09:10

wf9a5m75


check your error code first;if the code is :ERR_CLEARTEXT_NOT_PERMITTED; Try to add android:usesCleartextTraffic="true" to you application tag in your AndroidManifest.xml like below:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
like image 1
Kaeson Avatar answered Oct 30 '22 08:10

Kaeson