WebSocket
对象提供了用于创建和管理 WebSocket 连接,以及可以通过该连接发送和接收数据的 API。
构造函数节 WebSocket(url[, protocols])
返回一个 WebSocket
对象
Constant Value WebSocket.CONNECTING
0
WebSocket.OPEN
1
WebSocket.CLOSING
2
WebSocket.CLOSED
3
以上是WebSocket 构造函数的原型中存在的一些常量,可通过 WebSocket.readyState
对照上述常量判断 WebSocket 连接 当前所处的状态
const socket = new WebSocket ( 'ws://localhost:8080' ) ;
socket. addEventListener ( 'open' , function ( event ) {
socket. send ( 'Hello Server!' ) ;
} ) ;
socket. addEventListener ( 'message' , function ( event ) {
console. log ( 'Message from server ' , event. data) ;
} ) ;
浏览器兼容性节 Update compatibility data on GitHub Desktop Mobile Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet WebSocket
Full support YesFull support YesFull support 11Notes 打开 Full support YesFull support YesFull support YesFull support YesFull support YesFull support 14Notes 打开 Full support YesFull support Yes? WebSocket()
constructorFull support Yes? Full support 7打开 ? Full support Yes? Full support YesFull support YesFull support 7? ? ? binaryType
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? bufferedAmount
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? close
Full support YesFull support 12Full support 8打开 ? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? close
eventFull support YesFull support 12Full support Yes? Full support Yes? Full support YesFull support YesFull support Yes? ? ? error
eventFull support YesFull support 12Full support Yes? Full support Yes? Full support YesFull support YesFull support Yes? ? ? extensions
Full support YesFull support 12Full support 8? Full support YesFull support YesFull support YesFull support YesFull support 8Full support YesFull support Yes? message
eventFull support YesFull support 12Full support Yes? Full support Yes? Full support YesFull support YesFull support Yes? ? ? onclose
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? onerror
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? onmessage
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? onopen
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? open
eventFull support YesFull support 12Full support Yes? Full support Yes? Full support YesFull support YesFull support Yes? ? ? protocol
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? Supports protocol as specified by RFC 6455 Full support 16Full support 12Full support 11Full support 10Full support 15Full support 6Full support YesFull support 18Full support 14Full support 14Full support 6? readyState
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? send
Full support YesFull support 12Full support 18Notes 打开 ? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? url
Full support YesFull support 12Full support Yes? Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes? Available in workers Full support YesFull support YesFull support 37? ? ? Full support YesFull support YesFull support 37? ? ?
Legend Full support
Full support
Compatibility unknown
Compatibility unknown
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.