WebSocket

WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

To construct a WebSocket , use the WebSocket() 构造函数。

构造函数

WebSocket(url[, protocols])
Returns a newly created WebSocket 对象。

常量

常量
WebSocket.CONNECTING 0
WebSocket.OPEN 1
WebSocket.CLOSING 2
WebSocket.CLOSED 3

特性

WebSocket.binaryType
The binary data type used by the connection.
WebSocket.bufferedAmount 只读
The number of bytes of queued data.
WebSocket.extensions 只读
The extensions selected by the server.
WebSocket.onclose
An event listener to be called when the connection is closed.
WebSocket.onerror
An event listener to be called when an error occurs.
WebSocket.onmessage
An event listener to be called when a message is received from the server.
WebSocket.onopen
An event listener to be called when the connection is opened.
WebSocket.protocol 只读
The sub-protocol selected by the server.
WebSocket.readyState 只读
The current state of the connection.
WebSocket.url 只读
The absolute URL of the WebSocket.

方法

WebSocket.close([code[, reason]])
Closes the connection.
WebSocket.send(data)
Enqueues data to be transmitted.

Events

监听这些事件使用 addEventListener() 或通过把事件监听器赋值给 on eventname 特性为此接口。

close
Fired when a connection with a WebSocket is closed.
Also available via the onclose property
error
Fired when a connection with a WebSocket has been closed because of an error, such as when some data couldn't be sent.
Also available via the onerror 特性。
message
Fired when data is received through a WebSocket .
Also available via the onmessage 特性。
open
Fired when a connection with a WebSocket is opened.
Also available via the onopen 特性。

范例

// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
// Connection opened
socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
});
// Listen for messages
socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);
});
					

规范

规范 状态
HTML Living Standard
The definition of 'WebSocket' in that specification.
实时标准

浏览器兼容性

更新 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 Chrome 4 Edge 12 Firefox 11
注意事项
11
注意事项
bug 695635 .
7 — 11
Prefixed 注意事项
Prefixed Implemented with the vendor prefix: Moz
Message size limited to 16 MB (see bug 711205 ).
4 — 6
注意事项
Message size limited to 16 MB (see bug 711205 ).
IE 10 Opera 12.1 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 14
注意事项
14
注意事项
bug 695635 .
7 — 14
Prefixed 注意事项
Prefixed Implemented with the vendor prefix: Moz
Message size limited to 16 MB (see bug 711205 ).
4 — 6
注意事项
Message size limited to 16 MB (see bug 711205 ).
Opera Android 12.1 Safari iOS 4.2 Samsung Internet Android 1.0
WebSocket() 构造函数 Chrome Yes Edge ≤79 Firefox 7
7
4 — 7
注意事项
Parameter protocols not supported.
IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android 7 Opera Android ? Safari iOS ? Samsung Internet Android Yes
binaryType Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
bufferedAmount Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
close Chrome 4 Edge 12 Firefox 8
8
4 — 8
注意事项
Parameters not supported, see bug 674716 .
IE 10 Opera 12.1 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 8
8
4 — 8
注意事项
Parameters not supported, see bug 674716 .
Opera Android 12.1 Safari iOS 4.2 Samsung Internet Android 1.0
close event Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android ? Safari iOS ? Samsung Internet Android Yes
error event Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android ? Safari iOS ? Samsung Internet Android Yes
extensions Chrome Yes Edge 12 Firefox 8 IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 8 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
message event Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android ? Safari iOS ? Samsung Internet Android Yes
onclose Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onerror Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onmessage Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
onopen Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
open event Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android ? Safari iOS ? Samsung Internet Android Yes
protocol Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
Supports protocol as specified by RFC 6455 Chrome 16 Edge 12 Firefox 11 IE 10 Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 14 Opera Android 14 Safari iOS 6 Samsung Internet Android 1.0
readyState Chrome 43 Edge 12 Firefox 19 IE 10 Opera 30 Safari 10 WebView Android 43 Chrome Android 43 Firefox Android 19 Opera Android 30 Safari iOS 10 Samsung Internet Android 4.0
send Chrome 4 Edge 12 Firefox 18
注意事项
18
注意事项
bug 775368 .
11 — 18
注意事项
Only parameter of type ArrayBuffer and String supported.
8 — 11
注意事项
Only parameter of type String supported.
4 — 8
注意事项
Only parameter of type String supported. Returns boolean .
IE 10 Opera 12.1 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 18
注意事项
18
注意事项
bug 775368 .
14 — 18
注意事项
Only parameter of type ArrayBuffer and String supported.
8 — 14
注意事项
Only parameter of type String supported.
4 — 8
注意事项
Only parameter of type String supported. Returns boolean .
Opera Android 12.1 Safari iOS 4.2 Samsung Internet Android 1.0
url Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
Available in workers Chrome Yes Edge ≤18 Firefox 37 IE ? Opera ? Safari ? WebView Android Yes Chrome Android Yes Firefox Android 37 Opera Android ? Safari iOS ? Samsung Internet Android Yes

图例

完整支持
完整支持
兼容性未知
兼容性未知
见实现注意事项。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅