这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
RTCDataChannel接口代表在两者之间建立了一个双向数据通道的连接。
可以用RTCDataChannel.createDataChannel()
或者在现有的 RTCPeerConnection
上用 RTCDataChannelEvent
类型的 datachannel
事件接收,创建出 RTCDataChannel类型的对象。
这个API在Gecko中被称作DataChannel而不是标准的'RTCDataChannel'。
属性
RTCDataChannel.label
只读- 返回一个包含有描述数据通道名字的
DOMString
。这个字段没有唯一性要求。 RTCDataChannel.ordered
只读- 返回一个
Boolean
对象,表示传递信息的顺序是否有保证。 RTCDataChannel.protocol
只读- 返回一个包含有正在使用的子协议的名称的
DOMString
,如果没有这样的子协议,返回"" RTCDataChannel.id
只读- 当
RTCDataChannel
对象被创建出来的时候,返回一个无符号short类型的数据,作为通道的标识id。 RTCDataChannel.readyState
只读- 返回枚举类型的 RTCDataChannelState,表示数据连接的状态,有以下几种类型:
"connecting"
is the state indicating that the underlying connection is not yet set up and active. This is the initial state of a data channel created withRTCPeerConnection.createDataChannel()
."open"
is the state indicating that the underlying connection is up and running. This is the initial state of a data channel dispatched in aRTCDataChannelEvent
."closing"
is the state indicating that the underlying connection is in the process of shutting down. No new sending task is accepting but the cached messages are in the process of being sent, or received."closed"
is the state indicating that the underlying connection has been shut down (or couldn't be established).
RTCDataChannel.bufferedAmount
只读- Returns an
unsigned long
containing the amount of bytes that have been queued for sending: that is the amount of data requested to be transmitted viaRTCDataChannel.send()
that has not been sent yet. Note that if the channel isclosed
, the buffering continues. RTCDataChannel.binaryType
- Is a
DOMString
indicating the type of binary data transmitted by the connection. This should be either"blob"
ifBlob
objects are being used or"arraybuffer"
ifArrayBuffer
objects are being used. Initially it is set to"blob"
. RTCDataChannel.maxPacketLifeType
只读- Is an
unsigned short
indicating the length in milliseconds of the window in when messaging happens in unreliable mode. RTCDataChannel.maxRetransmits
只读- Is an
unsigned short
indicating the maximum amount of retransmissions that can happen when messaging happens in unreliable mode. RTCDataChannel.negotiated
只读- Is a
Boolean
indicating if the channel has been negotiated by the application, or not. DataChannel.reliable
只读- Is a
Boolean
indicating if the connection can send message in unreliable mode. DataChannel.stream
只读- Is an obsolete synonym for
RTCDataChannel.id
.
Event Handlers
RTCDataChannel.onopen
- Is the event handler called when the
open
event is received. Such an event is sent when a the underlying data transport, that is the data connection, has been established. RTCDataChannel.onmessage
- Is the event handler called when the
message
event is received. Such an event is sent when a message is available on the data connection. RTCDataChannel.onclose
- Is the event handler called when the
close
event is received. Such an event is sent when the underlying data transport has been closed. RTCDataChannel.onerror
- Is the event handler called when the
error
event is received. Such an event is sent when an error has been encountered.
Methods
RTCDataChannel.close()
- Closes the channel. The closing is done in a non abrupt way. The
state
of the channel is set to"closing"
, the messages not yet sent are sent, then the channel is closed. RTCDataChannel.send()
- Sends the data in parameter over the channel. The data can be a
DOMString
, aBlob
, anArrayBuffer
or anArrayBufferView
.
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
Specification | Status | Comment |
---|---|---|
WebRTC 1.0: Real-time Communication Between Browsers RTCDataChannel |
Working Draft | Initial specification. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) [1] | 未实现 | (Yes) | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | ? | 未实现 | ? | ? |
[1] The interface is called DataChannel
and not RTCDataChannel