What is the best way to detect websocket support using Javascript?
This is the shortest solution and is used by Modernizr. Simply add this to your code
supportsWebSockets = 'WebSocket' in window || 'MozWebSocket' in window;
then you can use it by running
if (supportsWebSockets) { // run web socket code}
I think the Modernizr library is what you are looking for: http://modernizr.com/
Once you include the library on your page, you can use a simple check like:
if(Modernizr.websockets){ // socket to me baby}
after reading @gzost's response.. I started tinkering.. since nothing else can properly detect WS's on my android phone... even websocket.org says i have it, but then fails to connect.
Anyways, try this workaround.. seems to properly detect it on/off with chrome, FF, safari and the default android browser.
var has_ws=0;function checkWebSocket(){ try{ websocket = new WebSocket("ws:websocket.org"); websocket.close(''); }catch(e){ //throws code 15 if has socket to me babies has_ws=1; }}$(document).ready(function(){ checkWebSocket();});