RTCDataChannel: close() 方法
RTCDataChannel.close() 方法用于关闭 RTCDataChannel。任何一方都可以调用此方法来发起通道关闭。
数据通道的关闭并非瞬时完成。关闭连接的大部分过程是异步处理的;您可以通过监听数据通道上的 close 事件来检测通道何时已完成关闭。
调用此方法后发生的事件顺序:
- 将
RTCDataChannel.readyState设置为closing。 - 建立一个后台任务来处理以下剩余步骤,并且
close()方法返回给调用者。 - 传输层处理任何已缓冲的消息;协议层决定是发送还是丢弃它们。
- 底层数据传输被关闭。
- 将
RTCDataChannel.readyState属性设置为closed。 - 如果传输关闭时发生错误,则
RTCDataChannel会收到一个error事件,其name设置为NetworkError。 - 向通道发送一个
close事件。
语法
js
close()
参数
无。
返回值
无(undefined)。
示例
js
const pc = new RTCPeerConnection();
const dc = pc.createDataChannel("my channel");
dc.onmessage = (event) => {
console.log(`received: ${event.data}`);
dc.close(); // We decided to close after the first received message
};
dc.onopen = () => {
console.log("datachannel open");
};
dc.onclose = () => {
console.log("datachannel close");
};
// Now negotiate the connection and so forth…
规范
| 规范 |
|---|
| WebRTC:浏览器中的实时通信 # dom-rtcdatachannel-close |
浏览器兼容性
加载中…