WebSocket 对象提供了用于创建和管理 WebSocket 连接,以及可以通过该连接发送和接收数据的 API。

构造函数

WebSocket(url[, protocols])

返回一个 WebSocket 对象

属性

WebSocket.binaryType
使用二进制的数据类型连接
WebSocket.bufferedAmount 只读
未发送至服务器的字节数
WebSocket.extensions 只读
服务器选择的扩展
WebSocket.onclose
用于指定连接关闭后的回调函数
WebSocket.onerror
用于指定连接失败后的回调函数
WebSocket.onmessage
用于指定当从服务器接受到信息时的回调函数
WebSocket.onopen
用于指定连接成功后的回调函数
WebSocket.protocol 只读
服务器选择的下属协议
WebSocket.readyState 只读
当前的链接状态
WebSocket.url 只读
WebSocket 的绝对路径

方法

WebSocket.close([code[, reason]])
关闭当前链接
WebSocket.send(data)
向服务器发送数据

示例

// 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
WebSocket
Living Standard 初始定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 11
Notes
Full support 11
Notes
Notes See bug 695635.
No support 7 — 11
Prefixed Notes
Prefixed Implemented with the vendor prefix: Moz
Notes Message size limited to 16 MB (see bug 711205).
No support 4 — 6
Notes
Notes Message size limited to 16 MB (see bug 711205).
IE ? Opera Full support YesSafari Full support YesWebView Android ? Chrome Android ? Edge Mobile Full support YesFirefox Android Full support 14
Notes
Full support 14
Notes
Notes See bug 695635.
No support 7 — 14
Prefixed Notes
Prefixed Implemented with the vendor prefix: Moz
Notes Message size limited to 16 MB (see bug 711205).
Opera Android ? Safari iOS Full support YesSamsung Internet Android ?
WebSocket() constructorChrome Full support YesEdge ? Firefox Full support 7
Full support 7
No support 4 — 7
Notes
Notes Parameter protocols not supported.
IE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support 7Opera Android ? Safari iOS ? Samsung Internet Android ?
binaryTypeChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
bufferedAmountChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
closeChrome Full support YesEdge ? Firefox Full support 8
Full support 8
No support 4 — 8
Notes
Notes Parameters not supported, see bug 674716.
IE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
extensionsChrome Full support YesEdge ? Firefox Full support 8IE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support 8Opera Android ? Safari iOS ? Samsung Internet Android ?
oncloseChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
onerrorChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
onmessageChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
onopenChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
protocolChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
readyStateChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
sendChrome Full support YesEdge ? Firefox Full support 18
Notes
Full support 18
Notes
Notes See bug 775368.
No support 11 — 18
Notes
Notes Only parameter of type ArrayBuffer and String supported.
No support 8 — 11
Notes
Notes Only parameter of type String supported.
No support 4 — 8
Notes
Notes Only parameter of type String supported. Returns boolean.
IE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
urlChrome Full support YesEdge ? Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?

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.

另见

文档标签和贡献者

标签: 
最后编辑者: Channely,