WebSocket for Android is a Cordova/PhoneGap plugin that makes it possible to use WebSockets (RFC 6455) on Android.
This is using Jetty under the terms of the Apache License v2.0.
- Java 1.6 or later
- Android 2.2 or later (recommend 4.0 or later)
- Cordova/PhoneGap 3.0.0 or later
The version for Cordova/Phonegap 2.x is here.
| version | ws protocol | wss protocol | text message | binary message |
|---|---|---|---|---|
| 2.2 | support | not support *1 |
support | limited support *2 |
| 2.3 | support | not support *1 |
support | limited support *2 |
| 3.x | -- *3 |
-- *3 |
-- *3 |
-- *3 |
| 4.0 | support | support | support | support |
| 4.1 | support | support | support | support |
| 4.2 | support | support | support | support |
| 4.3 | support | support | support | support |
| 4.4 | native support *4 |
native support *4 |
native support *4 |
native support *4 |
*1 Due to Android SSL issues.
*2 Supports Base64-encoded data only.
*3 May work. But not tested.
*4 In KitKat, WebSocket API has been officially supported. The native API of these devices is used in preference to this plugin.
Use the Cordova/PhoneGap Command-Line interface:
$ cordova plugin add https://github.com/knowledgecode/WebSocket-for-Android.gitor
$ phonegap plugin add https://github.com/knowledgecode/WebSocket-for-Android.git$ cordova plugin remove com.knowledgecode.cordova.websocket
$ cordova plugin add https://github.com/knowledgecode/WebSocket-for-Android.gitor
$ phonegap plugin remove com.knowledgecode.cordova.websocket
$ phonegap plugin add https://github.com/knowledgecode/WebSocket-for-Android.gitWhen you install this plugin, it adds INTERNET permission to AndroidManifest.xml. If you remove this plugin, the permission is also removed at the same time.
The WebSocket(url, protocols) constructor takes one or two arguments. The first argument, url, specifies the URL to which to connect. The second, protocols, is either a string or an array of strings.
A simple code is as follows:
document.addEventListener('deviceready', function () {
var ws = new WebSocket('ws://echo.websocket.org');
ws.onopen = function () {
console.log('open');
this.send('hello'); // transmit "hello" after connecting
};
ws.onmessage = function (event) {
console.log(event.data); // will be "hello"
this.close();
};
ws.onerror = function () {
console.log('error occurred!');
};
ws.onclose = function (event) {
console.log('close code=' + event.code);
};
}, false);And then, this plugin has options. Details are as follows:
WebSocket.pluginOptions = {
origin: 'http://websocket-is-fun.com',
maxConnectTime: 20000, // 20sec
maxTextMessageSize: 32768, // 32kb
maxBinaryMessageSize: 32768 // 32kb
};All these parameters are omissible. The origin will be set empty if omit it. The maxConnectTime is a wait time for connection. The default value will be 20,000 milliseconds if omit it. The maxTextMessageSize and the maxBinaryMessageSize are receivable maximum size from server. The default values will be 32,768 bytes if omit them.
To work with common source code for devices supporting the native API (such as Android 4.4 and iOS 6 or later), it is recommended to write as the following:
if (WebSocket.pluginOptions) {
WebSocket.pluginoptions = {
origin: 'http://chatterchatter.org'
};
}
var ws = new WebSocket('ws://chatterchatter.org');Transmits data to the server over the WebSocket connection. The data takes a string, a blob, or an arraybuffer.
In devices that are not supported both a blob and an arraybuffer, cannot transmit binary messages. (ex. Android 2.2 and 2.3)
If receives binary messages in unsupported devices, automatically encodes them to Base64.
ws.onmessage = function (event) {
// For example, the receiving data can be used as data URI scheme.
img.src = 'data:image/jpeg;base64,' + event.data;
};Closes the WebSocket connection or connection attempt, if any.
- updated Jetty WebSocket library
- added escaping of special characters (thanks to @odbol)
- cookie support (thanks to @ericfong)
- removed a second argument from the send() method
- forcing the WebSocket of plugin in Android 4.3 or lower (thanks to @rpastorvargas and @punj)
- bug fix
- bug fix
- change the way to set plugin options
- multiple subprotocol support
- readyState property support (thanks to @jrpereirajr)
- Cordova/Phonegap 3 support
- binary support
- event listener support
- more compliant with the WebSocket API requirements
- license change from MIT to Apache v2.0
- bug fix
- bug fix
- origin support (thanks to @rgillan)
- comply with the WebSocket API requirements
- first release
This plugin is available under the terms of the Apache License Version 2.0.