send() 方法在 RTCDataChannel interface sends data across the data channel to the remote peer. This can be done any time except during the initial process of creating the underlying transport channel. Data sent before connecting is buffered if possible (or an error occurs if it's not possible), and is also buffered if sent while the connection is closing or closed.

Different browsers have different limitations on the size of the message you can send. Specifications exist to define how to automatically fragment large messages, but not all browsers implement them, and those that do have various additional restrictions. This will get less complicated over time, but for now, if you have questions, see Understanding message size limits in Using WebRTC data channels .

句法

RTCDataChannel.send(data);
					

参数

data
The data to transmit across the connection. This may be a USVString Blob ArrayBuffer ,或 ArrayBufferView .

返回值

undefined .

异常

InvalidStateError
Since the data channel uses a separate transport channel from the media content, it must establish its own connection; if it hasn't finished doing so (that is, its readyState is "connecting") , this error occurs without sending or buffering the data .
NetworkError
指定 data would need to be buffered, and there isn't room for it in the buffer. In this scenario, the underlying transport is immediately closed.
TypeError
指定 data is too large for the other peer to receive. Since there are multiple techniques for breaking up large data into smaller pieces for transfer, it's possible to encounter scenarios in which the other peer does not support the same ones. For example, if one peer is a modern browser that supports using the EOR (End of Record) flag to indicate when a received message is the last piece of a multi-part object sent using send() . For more information about message size restrictions, see Understanding message size limits in Using WebRTC data channels .

范例

In this example, a routine called sendMessage() is created; it accepts an object as input and sends to the remote peer, over the RTCDataChannel , a JSON string with the specified object and a time stamp.

var pc = new RTCPeerConnection();
var dc = pc.createDataChannel("BackChannel");
function sendMessage(msg) {
  let obj = {
    "message": msg,
    "timestamp": new Date()
  }
  dc.send(JSON.stringify(obj));
}
					

规范

规范 状态 注释
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCDataChannel.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 56 Edge ≤79 Firefox Yes IE No Opera 43 Safari Yes WebView Android 56 Chrome Android 56 Firefox Android Yes Opera Android 43 Safari iOS Yes Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: