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
USVString
bufferedAmount
is increased by the number of bytes required to represent the UTF-8 string.
ArrayBuffer
bufferedAmount
by the requisite number of bytes.
Blob
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
bufferedAmount
by the requisite number of bytes.
INVALID_STATE_ERR
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. |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
send
|
Chrome 4 | Edge 12 |
Firefox
18
|
IE 10 | Opera 12.1 | Safari 5 | WebView Android ≤37 | Chrome Android 18 |
Firefox Android
18
|
Opera Android 12.1 | Safari iOS 4.2 | Samsung Internet Android 1.0 |
完整支持
见实现注意事项。