WebSocket.send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data. If the data can't be sent (for example, because it needs to be buffered but the buffer is full), the socket is closed automatically.

句法

WebSocket.send("Hello server!");
					

参数

data
The data to send to the server. It may be one of the following types:
USVString
A text string. The string is added to the buffer in UTF-8 format, and the value of bufferedAmount is increased by the number of bytes required to represent the UTF-8 string.
ArrayBuffer
You can send the underlying binary data used by a typed array object; its binary data contents are queued in the buffer, increasing the value of bufferedAmount by the requisite number of bytes.
Blob
Specifying a Blob enqueues the blob's raw data to be transmitted in a binary frame. The value of bufferedAmount is increased by the byte size of that raw data.
ArrayBufferView
You can send any JavaScript typed array object as a binary frame; its binary data contents are queued in the buffer, increasing the value of bufferedAmount by the requisite number of bytes.

Exceptions thrown

INVALID_STATE_ERR
The connection is not currently OPEN .
SYNTAX_ERR

The data is a string that has unpaired surrogates.

注意: Gecko's implementation of the send() method differs somewhat from the specification in Gecko 6.0 ; Gecko returns a boolean indicating whether or not the connection is still open (and, by extension, that the data was successfully queued or transmitted); this is corrected in Gecko 8.0 .

As of Gecko 11.0 , support for ArrayBuffer is implemented but not Blob 数据类型。

规范

规范 状态 注释
HTML 实时标准
The definition of 'WebSocket: send' in that specification.
实时标准 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
send Chrome 4 Edge 12 Firefox 18
18
bug 775368 .
不支持 11 — 18
Only parameter of type ArrayBuffer and 字符串 被支持。
不支持 8 — 11
Only parameter of type 字符串 被支持。
不支持 4 — 8
Only parameter of type 字符串 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 字符串 被支持。
不支持 8 — 14
Only parameter of type 字符串 被支持。
不支持 4 — 8
Only parameter of type 字符串 supported. Returns boolean .
Opera Android 12.1 Safari iOS 4.2 Samsung Internet Android 1.0

图例

完整支持

完整支持

见实现注意事项。

元数据

  • 最后修改:
  1. WebSocket
  2. 构造函数
    1. WebSocket()
  3. 特性
    1. binaryType
    2. bufferedAmount
    3. extensions
    4. onclose
    5. onerror
    6. onmessage
    7. onopen
    8. 协议
    9. readyState
    10. url
  4. 方法
    1. close()
    2. send()
  5. 事件
    1. error
    2. message
  6. 继承:
    1. EventTarget

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1