简体   繁体   中英

What is the best way to detect websocket support using Javascript?

I'm trying to use Javascript to detect if a web browser supports websockets, but using only feature-based detection, I'm getting false positives, so I added a user agent test to throw out Android devices instead, which I'm not happy about. I have a Samsung Galaxy Tab 2, and here's my detection code:

var isSupported = (("WebSocket" in window && window.WebSocket != undefined) ||
                   ("MozWebSocket" in window));

/* This line exists because my Galaxy Tab 2 would otherwise appear to have support. */
if (isSupported && navigator.userAgent.indexOf("Android") > 0)
  isSupported = false;

if (isSupported)
  document.write("Your browser supports websockets");
else
  document.write("Your browser does not support websockets");

This code seems to work with IE, Firefox, Safari (including iPhone/iPad), and Chrome. However, the feature-based check is returning true when I use the default browser of my Samsung Galaxy Tab 2, which is incorrect because that browser does not actually support websockets. Furthermore, I don't know how many other Android devices have this same issue, so at the moment, this is the best solution I'm aware of for detection.

Is there a better way to detect websocket support other than what I'm doing? I do realize that workarounds exist for Android, such as using a different browser, which means my user agent detection code as-is would not be a good thing. My goal is to not have to rely on the user agent in the first place.

Any suggestions?

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();
});

This page comes on top in google search.

In year 2016 cutting the mustard for modern WebSockets implementation (no prefixes such as MozWebSocket ) would be

if (
  'WebSocket' in window && window.WebSocket.CLOSING === 2
) {
 // supported
}

http://www.w3.org/TR/websockets/#the-websocket-interface

None of the above answers by itself was sufficient in my tests. The following code seems to be working fine:

    function nll( o ) { return CS.undefined === typeof o || null === o; }
    // ...
    function check_ws_object() {
        try {
            var websocket = new WebSocket( "wss://echo.websocket.org" );
            return true;
        } catch ( e ) { ; }
        return false;
    }
    //
    function check_support() {
        if ( !( WebSocket in window ) ) {
            if ( nll( window.WebSocket) ) {
                if ( !this.check_ws_object() ) {
                    alert( "This browser doesn't support HTML5 Web Sockets!" );
                    return false;
                }
            }
        }
        return true;
    },

The above tests are sorted, so that the faster ones come first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM