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

后端 未结 5 1244
慢半拍i
慢半拍i 2020-12-31 04:27

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 t

5条回答
  •  被撕碎了的回忆
    2020-12-31 04:44

    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

提交回复
热议问题