Added WebSocket support for PhoneGap Android platform, making default the Socket.io preferred channel selection Advertise
With Socket.io as the real-time push preference for cross-browser platforms, testing on every major browser does have a good real-time performance. Here for the promotion of Socketio-netty server-side implementation ha, do ad, and warm up a bit:
Socketio-netty: Another Socket.io server-side implementation, compatible with 0.9-1.0 version ~
Demonstration purposes
We want to build a common browser on the market can be normal operation of the collective chat application, to ensure that the ie6+,firefox,chrome,safari,opear,ios,android and so can operate normally, according to the specific environment automatically select the best communication channel.
Well, since it's a cross-browser platform, that natural selection Socket.io (client js). It is also the focus of this article, the end of this article is to let the Socket.io (client JS) version in the PhoneGap packaging Android APK program can use the WebSocket protocol, in order to achieve fast exchange of data for the purpose of improving Exchange performance.
It also ensures that our demonstration applications are written as well as possible and run everywhere.
Fortunately, with Socket.io (client js) + socketio-netty (Socket.io server-side Java implementation) + PHONEGAP, we build a variety of highly interactive, cross-browser, cross-mobile application of HTML applications, is a good choice.
Also, this article demonstrates that the project is only for demonstrating its functionality and does not guarantee the style.
Phonegap
Website
The official website is defined as:
PhoneGap is a HTML5 app platform that allows I author native applications with Web technologies and get ACC ESS to APIs and app stores.
The Chinese translation is:
PhoneGap is an open-source development framework that uses HTML,CSS and JavaScript to build cross-platform mobile applications
Using HTML + CSS + JavaScript to build cross-platform mobile references is really good and recommended.
Better, can take advantage of its cloud building Services (HTTPS://BUILD.PHONEGAP.COM/APPS), the machine after writing a good application (to ensure that the home page for index.html, involving the css/js stored together), packaged into a zip, upload, It is very convenient for us to automatically build deployment packages under different platforms.
On the surface, everything is perfect, but after deploying to the Android system, it is found that Socket.io cannot use websocket bidirectional channels, socket.io default to Xhr-long polling communication mode. Some helpless.
In the case of real-time interactive data volume, compared to Xhr-long polling, Jsonp Polling,websocket/flashsocket has the speed advantage that can not be exceeded, while the two-way data transmission channel, through observation may be clearly felt.
causes
- My Android system is 2.3, and its native browser does not support the WebSocket Communication protocol (Ucweb,qq,opear Mini supports a more complete HTML5 specification).
- The PhoneGap converted APK package will call Android's built-in browser, which results in websocket not working.
- According to research Android 2/3.* native browser does not support WebSocket, as for Android 4.*, has not been tested.
How to confirm the browser support for HTML5, browser access http://html5test.com can query the support of HTML5, as well as running points.
Well, according to rumors, PhoneGap will add websocket support for Android after version 2.0, but the current version is 1.7.
How to solve
Animesh Kumar developed the WEBSOCKET-ANDROID-PHONEGAP project, has done so PHONEGAP support WebSocket client protocol, using Java NIO to write WebSocket client protocol connection, At the same time PhoneGap supports custom components, and supports the mutual invocation of JS and Java code in an open architecture, which facilitates the webscoket.js of camouflage.
Its somewhat dwr taste, but more flexible.
There's also a simple Socket.io Android client implementation:
Https://github.com/koush/android-websockets#readme
If you are interested, please refer to it.
This plan uses Netty constructs a websocket client, then unifies JS and so on, but has the open source realization, no longer behind closed doors.
- Create a new Android Project project in Eclipse Chatdemo
- Animesh Kumar websocket-android-phonegap Project Java file into a Phonegap-websocket-support.jar package, stored on Android In the Libs directory of Project
- Store the websocket.js in the Assets/www/js directory
- Modify Project Startup class App.java
- Add <script src= "Js/websocket.js" ></script>
Simple Description
1. App.java After modification:
Make sure to inherit Droidgap and specify the binding statement:
Binding WebSocket Support
Appview.addjavascriptinterface (New Websocketfactory (Appview),
"Websocketfactory");
The Java end is set.
2. Client-side modifications
Need to do some hands on the HTML page, priority loading websocket.js to set some environment variables, so Socket.io can detect websocket support.
Initialization code for Websocekt.js:
You need to be aware of its initialization code:
Window object
var global = window;
WebSocket Object. All listener methods is cleaned up!
var WebSocket = Global. WebSocket = function (URL)
......
Socket.io Client WebSocket Code snippet:
Take a look at the WebSocket's detection function:
Ws.check = function () {
Return (' WebSocket ' in global &&! __addtask ' in WebSocket))
|| ' Mozwebsocket ' in global;
};
Naturally, the custom websocket.js and Socket.io can be naturally linked together.
Therefore, the required page JS load order is:
<!--Android platform needs to be added, other mobile platforms, such as iOS, do not need-
<!--must be in front of socket.io.min.js--
<script src= "Js/websocket.js" ></script>
<script src= "Js/socket.io.min.js" ></script>
On the HTML page side, we just need to add a line <script src= "js/websocket.js" ></script> reference, To make the Android platform under the Socket.io priority Choice WebSocket, very simple, also very use.
As for other platforms, you do not need to consider this, just package the/chatdemo/assets/www directory into a zip package (remember to <script src= "Js/websocket.js" ></script> to remove), Upload to the cloud build platform to build automatically.
Small Summary
PhoneGap the Android platform to support WebSocket, the steps are simple:
- Build Android Project under Eclipse
- Copy jar and socekt.js to the appropriate directory
- Modify App.java (other Android Boot class, method name is different, but method body is consistent)
- On the homepage or the desired page, in front of Socket.io JS Medical, add <script src= "js/websocket.js" ></script> reference can be
Demo Code
- The Socket.io framework's built-in chat chat demo and Socketio-netty provide the same, except for server-side implementations
- Simply packaged as an Android project and used only for demonstration purposes, so the interface is a bit large
- Need a server side, Socket.io or Socketio-netty can
- This article android demo chat download Download
- Phonegap-websocket-support.jar
Added websocket support for the PhoneGap Android platform, making the default a Socket.io preferred pass