RTCDataChannel: readyState 属性
只读 RTCDataChannel 属性 readyState 返回一个字符串,用于指示数据通道底层数据连接的状态。
值
一个字符串,指示底层数据传输的当前状态,其值是以下值之一:
connecting-
用户代理(浏览器)正在创建底层数据传输;这是由
RTCPeerConnection.createDataChannel()创建的新RTCDataChannel在启动连接过程的对等方上的状态。 open-
底层数据传输已建立,并且可以在其上传输双向数据。这是 WebRTC 层创建的新
RTCDataChannel的默认状态,当远程对等方创建通道并将其通过datachannel事件传递给站点或应用时。 closing-
正在开始关闭底层数据传输的过程。不再可能排队发送新消息,但先前排队的消息可能仍在进入
closed状态之前发送或接收。 closed-
底层数据传输已关闭,或者建立连接的尝试失败。
示例
js
const dataChannel = peerConnection.createDataChannel("File Transfer");
const sendQueue = [];
function sendMessage(msg) {
switch (dataChannel.readyState) {
case "connecting":
console.log(`Connection not open; queueing: ${msg}`);
sendQueue.push(msg);
break;
case "open":
sendQueue.forEach((msg) => dataChannel.send(msg));
break;
case "closing":
console.log(`Attempted to send message while closing: ${msg}`);
break;
case "closed":
console.log("Error! Attempt to send while connection closed.");
break;
}
}
规范
| 规范 |
|---|
| WebRTC:浏览器中的实时通信 # dom-datachannel-readystate |
浏览器兼容性
加载中…