RTCDataChannel - Web APIs 编辑

Event handlers

The RTCDataChannel interface represents a network channel which can be used for bidirectional peer-to-peer transfers of arbitrary data. Every data channel is associated with an RTCPeerConnection, and each peer connection can have up to a theoretical maximum of 65,534 data channels (the actual limit may vary from browser to browser).

To create a data channel and ask a remote peer to join you, call the RTCPeerConnection's createDataChannel() method. The peer being invited to exchange data receives a datachannel event (which has type RTCDataChannelEvent) to let it know the data channel has been added to the connection.

Properties

Also inherits properties from: EventTarget

binaryType This is an experimental API that should not be used in production code.
The property binaryType on the RTCDataChannel interface is a DOMString which specifies the type of JavaScript object which should be used to represent binary data received on the RTCDataChannel. Values allowed by the WebSocket.binaryType property are also permitted here: "blob" if Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used. The default is "blob".
bufferedAmount This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property bufferedAmount returns the number of bytes of data currently queued to be sent over the data channel.
bufferedAmountLowThreshold This is an experimental API that should not be used in production code.
The RTCDataChannel property bufferedAmountLowThreshold is used to specify the number of bytes of buffered outgoing data that is considered "low." The default value is 0.
id This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property id returns an ID number (between 0 and 65,534) which uniquely identifies the RTCDataChannel.
label This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property label returns a DOMString containing a name describing the data channel. These labels are not required to be unique.
maxPacketLifeTime This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property maxPacketLifeTime returns the amount of time, in milliseconds, the browser is allowed to take to attempt to transmit a message, as set when the data channel was created, or null.
maxRetransmits This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property maxRetransmits returns the maximum number of times the browser should try to retransmit a message before giving up, as set when the data channel was created, or null, which indicates that there is no maximum.
negotiated undefined
The read-only RTCDataChannel property negotiated indicates whether the RTCDataChannel's connection was negotiated by the Web app (true) or by the WebRTC layer (false).
ordered This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property ordered indicates whether or not the data channel guarantees in-order delivery of messages; the default is true, which indicates that the data channel is indeed ordered.
protocol This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property protocol returns a DOMString containing the name of the subprotocol in use. If no protocol was specified when the data channel was created, then this property's value is "" (the empty string).
readyState This is an experimental API that should not be used in production code. undefined
The read-only RTCDataChannel property readyState returns an enum of type RTCDataChannelState which indicates the state of the data channel's underlying data connection.
reliable This is an experimental API that should not be used in production code. This is an obsolete API and is no longer guaranteed to work. undefined
The read-only RTCDataChannel property reliable indicates whether or not the data channel is reliable.
stream This API has not been standardized. This is an obsolete API and is no longer guaranteed to work. undefined
The deprecated (and never part of the official specification) read-only RTCDataChannel property stream returns an ID number (between 0 and 65,535) which uniquely identifies the RTCDataChannel.

Also inherits event handlers from: EventTarget

onbufferedamountlow This is an experimental API that should not be used in production code.
The RTCDataChannel.onbufferedamountlow property is an EventHandler which specifies a function the browser calls when the bufferedamountlow event is sent to the RTCDataChannel. This event, which is represented by a simple Event object, is sent when the amount of data buffered to be sent falls to or below the threshold specified by the channel's RTCDataChannel.bufferedAmountLowThreshold.
onclose This is an experimental API that should not be used in production code.
The RTCDataChannel.onclose property is an EventHandler which specifies a function to be called by the browser when the close event is received by the RTCDataChannel. This is a simple Event which indicates that the data channel has closed down.
onclosing This is an experimental API that should not be used in production code.
The RTCDataChannel.onclosing property is an EventHandler which specifies a function to be called by the browser when the closing event is received by the RTCDataChannel. This is a simple Event which indicates that the data channel is being closed, that is, RTCDataChannel transitions to "closing" state. For example, after RTCDataChannel.close was called but the underlying data transport might not have been closed yet.
onerror This is an experimental API that should not be used in production code.
The RTCDataChannel.onerror property is an EventHandler which specifies a function to be called when the error event is received. When an error occurs on the data channel, the function receives as input an ErrorEvent object describing the error which occurred.
onmessage This is an experimental API that should not be used in production code.
The RTCDataChannel.onmessage property stores an EventHandler which specifies a function to be called when the message event is fired on the channel. This event is represented by the MessageEvent interface. This event is sent to the channel when a message is received from the other peer.
onopen This is an experimental API that should not be used in production code.
The RTCDataChannel.onopen property is an EventHandler which specifies a function to be called when the open event is fired; this is a simple Event which is sent when the data channel's underlying data transport—the link over which the RTCDataChannel's messages flow—is established or re-established.

Methods

Also inherits methods from: EventTarget

close()
The RTCDataChannel.close() method closes the RTCDataChannel. Either peer is permitted to call this method to initiate closure of the channel.
send()
The send() method of the RTCDataChannel interface sends data across the data channel to the remote peer.

Events

bufferedamountlow
Sent to the channel's onbufferedamountlow event handler when the number of bytes of data in the outgoing data buffer falls below the value specified by bufferedAmountLowThreshold.
close
Sent to the onclose event handler when the underlying data transport closes.
error
Sent to the onerror event handler when an error occurs on the data channel.
message
Sent to the onmessage event handler when a message has been received from the remote peer. The message contents can be found in the event's data property.
open
Sent to the onopen event handler when the data channel is first opened, or when an existing data channel's underlying connection re-opens.

Data format

The underlying data format is defined by the IEEE draft specification draft-ietf-mmusic-sctp-sdp. The current format specifies its protocol as either "UDP/DTLS/SCTP" (UDP carrying DTLS carrying SCTP) or "TCP/DTLS/SCTP" (TCP carrying DTLS carrying SCTP). Older browsers may only specify "DTLS/SCTP".

Example

var pc = new RTCPeerConnection();
var dc = pc.createDataChannel("my channel");

dc.onmessage = function (event) {
  console.log("received: " + event.data);
};

dc.onopen = function () {
  console.log("datachannel open");
};

dc.onclose = function () {
  console.log("datachannel close");
};

Specifications

SpecificationStatusComment
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCDataChannel' in that specification.
Candidate RecommendationInitial specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:101 次

字数:23619

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文