Reputation: 252
I've been using web-socket-js for websocket support across devices. Works great in all IOS devices as they have native web-socket support, and works fairly consistently in android devices when you have flash installed. Now, Android ICS 4.03 and above claims to support native web-sockets. Window.WebSocket is defined, but I'm having no luck opening the socket. Does anyone know why? What is the problem with ICS native websockets? What protocol do they use? Has anyone come up with a better solution?
=Update=
There is a hacky way to determine if the websocket actually works, and then fallback to flash. To do this you have to change the web-socket-js code to check if it is an android client. then, before using the web-socket, try to connect to a port on the local machine. Then check the protocol property of the websocket. If this is defined, then you are good to go with native, otherwise fall back to flash. Still looking for a better way, but here is the hack that I am using now:
eg:
var isAndroid = navigator.userAgent.match(/Android/i) != null;
var isChrome = navigator.userAgent.match(/Chrome/i) != null;
// assume if its safari, that they use normal websockets.
if ( isChrome || (!isAndroid && window.WebSocket)) {
logger.log("Will attempt to use Websockets natively. 1");
return;
}
// check if we need a websocket fallback
//if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
if ( isAndroid ) {
try {
var testSock = new window.WebSocket("ws://localhost:1474");
if (testSock.protocol != undefined) {
testSock.close();
logger.log("Will attempt to use Websockets natively. 2");
return;
} else {
// use flash
}
} catch (e) {
// if there was an error we need to use the flash fallback.
}
}
logger.log("Native Websockets unavailable, trying Flash fallback...");
Upvotes: 2
Views: 7694
Reputation: 1795
Folks, don't forget when emulating a device, avoid defining "localhost" in your config.xml of your android project to access your locally run WS-Server. Use the defined 10.0.2.2 address which is translated to 127.0.0.1 (and make sure your WS-Server responds to it). http://developer.android.com/tools/devices/emulator.html#emulatornetworking
Upvotes: 1
Reputation: 252
So, as it turns out, the hack mentioned in my question doesn't actually always works. Some android devices with 4.0.3 seem to support native web-sockets and others don't. I'm trying to determine if this difference could be between devices that came with 4.0.3 and those that were upgraded. I now check if flash player is supported first, if it is then I use it. If not, then I check the android version. If it is greater than 4.0 I suggest that the user tries to load the site in chrome. Otherwise, if window.WebSocket is defined I attempt to use it.
Upvotes: 4
Reputation: 22011
Chrome for Android supports WS (RFC6455) - it's available on ICS onwards, but not the standard browser.
WS (RFC6455) is supported by Firefox Mobile on Android devices with at least ARMv7.
WS (Hixie76) is supported by Opera Mobile on most Android devices.
Push Google for:
Upvotes: 2
Reputation: 73119
Android releases up through 4.X only supports WebSockets via a Flash emulator like web-socket-js Hopefully 4.1 (ICS) will introduce native WebSocket support.
Perhaps window.WebSocket is being defined because you are loading the emulator?
Upvotes: 0