如何关闭 webrtc 数据通道?

how to close a webrtc datachannel?

我无法使用以下方法。

因为我只使用数据通道(不使用 getUserMedia)

<script>
    peerConnection.removeStream(remoteStream)
    remoteVideo.onerror = null;
    remoteVideo.pause();
    remoteVideo.src = undefined;
    remoteStream.stop();
    remoteStream.onended = null;
    remoteStream = null;

    peerConnection.removeStream(localStream)

    localVideo.onerror = null;
    localVideo.pause();
    localVideo.src = undefined;
    localStream.stop();
    localStream.onended = null;
    localStream = null;
</script>

有没有办法关闭数据通道?

要关闭 RTCDataChannel,您可以调用 close() - 这也可以在 RTCPeerConnection 上调用,这将关闭在对等连接上创建的所有数据通道。

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

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

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

dc.close();
pc.close();