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.
Also inherits properties from:
EventTarget
binaryType
binaryType
在
RTCDataChannel
接口是
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
只读
RTCDataChannel
property
bufferedAmount
returns the number of bytes of data currently queued to be sent over the data channel.
bufferedAmountLowThreshold
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
只读
RTCDataChannel
property
id
returns an ID number (between 0 and 65,534) which uniquely identifies the
RTCDataChannel
.
label
只读
RTCDataChannel
property
label
返回
DOMString
containing a name describing the data channel. These labels are not required to be unique.
maxPacketLifeTime
只读
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
只读
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
只读
RTCDataChannel
property
negotiated
indicates whether the
RTCDataChannel
's connection was negotiated by the Web app (
true
) or by the WebRTC layer (
false
).
ordered
只读
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.
协议
只读
RTCDataChannel
property
协议
返回
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
只读
RTCDataChannel
property
readyState
returns an enum of type
RTCDataChannelState
which indicates the state of the data channel's underlying data connection.
reliable
RTCDataChannel
property
reliable
indicates whether or not the data channel is reliable.
stream
RTCDataChannel
property
stream
returns an ID number (between 0 and 65,535) which uniquely identifies the
RTCDataChannel
.
Also inherits event handlers from:
EventTarget
onbufferedamountlow
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
事件
object, is sent when the amount of data buffered to be sent falls to or below the threshold specified by the channel's
bufferedAmountLowThreshold
.
onclose
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
事件
which indicates that the data channel has closed down.
onclosing
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
事件
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
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
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
RTCDataChannel.onopen
property is an
EventHandler
which specifies a function to be called when the
open
event is fired; this is a simple
事件
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.
Also inherits methods from:
EventTarget
close()
RTCDataChannel.close()
method closes the
RTCDataChannel
. Either peer is permitted to call this method to initiate closure of the channel.
send()
send()
方法在
RTCDataChannel
interface sends data across the data channel to the remote peer.
bufferedamountlow
onbufferedamountlow
event handler when the number of bytes of data in the outgoing data buffer falls below the value specified by
bufferedAmountLowThreshold
.
close
onclose
event handler when the underlying data transport closes.
error
onerror
event handler when an error occurs on the data channel.
message
onmessage
event handler when a message has been received from the remote peer. The message contents can be found in the event's
data
特性。
open
onopen
event handler when the data channel is first opened, or when an existing data channel's underlying connection re-opens.
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"
.
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");
};
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCDataChannel' in that specification. |
候选推荐 | 最初的规范。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
RTCDataChannel
|
Chrome Yes | Edge ≤79 |
Firefox
24
|
IE No | Opera Yes | Safari Yes | WebView Android 4.4 | Chrome Android 29 |
Firefox Android
24
|
Opera Android Yes | Safari iOS Yes | Samsung Internet Android 2.0 |
binaryType
|
Chrome Yes | Edge ≤79 | Firefox 18 | IE No | Opera Yes | Safari Yes | WebView Android 4.4 | Chrome Android 29 | Firefox Android 18 | Opera Android Yes | Safari iOS No | Samsung Internet Android 2.0 |
bufferedAmount
|
Chrome 56 | Edge ≤79 | Firefox 18 | IE No | Opera 43 | Safari Yes | WebView Android 56 | Chrome Android 56 | Firefox Android 18 | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
bufferedamountlow
event
|
Chrome
57
|
Edge
≤79
|
Firefox Yes | IE No |
Opera
44
|
Safari Yes |
WebView Android
57
|
Chrome Android
57
|
Firefox Android Yes |
Opera Android
43
|
Safari iOS No |
Samsung Internet Android
7.0
|
bufferedAmountLowThreshold
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari Yes | WebView Android 56 | Chrome Android 56 | Firefox Android No | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
close
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
close
event
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari ? | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
error
event
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari ? | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
id
|
Chrome 56 | Edge ≤79 | Firefox No | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android No | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
label
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
maxPacketLifeTime
|
Chrome 56 | Edge ≤79 | Firefox 62 | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android 62 | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
maxRetransmits
|
Chrome 56 | Edge ≤79 | Firefox 62 | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android 62 | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
message
event
|
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 |
negotiated
|
Chrome 56 | Edge ≤79 | Firefox 68 | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android 68 | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
onbufferedamountlow
|
Chrome
57
|
Edge
≤79
|
Firefox Yes | IE No |
Opera
44
|
Safari Yes |
WebView Android
57
|
Chrome Android
57
|
Firefox Android Yes |
Opera Android
43
|
Safari iOS No |
Samsung Internet Android
7.0
|
onclose
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
onclosing
|
Chrome 81 | Edge 81 | Firefox No | IE No | Opera 68 | Safari No | WebView Android 81 | Chrome Android 81 | Firefox Android No | Opera Android ? | Safari iOS No | Samsung Internet Android ? |
onerror
|
Chrome 56 | Edge ≤79 | Firefox No | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android No | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
onmessage
|
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 |
onopen
|
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 |
open
event
|
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 |
ordered
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
priority
|
Chrome No | Edge No | Firefox ? | IE No | Opera ? | Safari ? | WebView Android No | Chrome Android No | Firefox Android ? | Opera Android ? | Safari iOS No | Samsung Internet Android No |
协议
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
readyState
|
Chrome 56 | Edge ≤79 | Firefox Yes | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android Yes | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
reliable
弃用
|
Chrome 56 | Edge ≤79 | Firefox No | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android No | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
| Support for sctp-sdp-21 format | Chrome 58 | Edge ≤79 | Firefox 63 | IE No | Opera ? | Safari No | WebView Android 58 | Chrome Android 58 | Firefox Android 63 | Opera Android ? | Safari iOS No | Samsung Internet Android 7.0 |
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 |
stream
弃用
非标
|
Chrome 56 | Edge ≤79 | Firefox No | IE No | Opera 43 | Safari No | WebView Android 56 | Chrome Android 56 | Firefox Android No | Opera Android 43 | Safari iOS No | Samsung Internet Android 6.0 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
使用非标名称。
RTCDataChannel
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnection
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsReport
RTCTrackEvent