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...");
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.