我试图检测 RTCPeerConnection 的另一端何时断开连接。目前我正在使用我的 RTCPeerConnection 对象执行以下操作:
rtcPeerConnection.oniceconnectionstatechange = () => {
const state = rtcPeerConnection.iceConnectionState;
if (state === "failed" || state === "closed") {
// connection to the peer is lost and unsalvageable, run cleanup code
} else if (state === "disconnected") {
// do nothing in the "disconnected" state as it appears to be a transient
// state that can easily return to "connected" - I've seen this with Firefox
}
};
这似乎在我非常简单的网络条件下进行的有限测试中有效,但MDN的以下内容让我停下来,它可能不会在生产中保持不变:
当然,“断开”和“关闭”不一定表示错误;这些可能是正常 ICE 协商的结果,因此请务必妥善处理(如果有的话)。
如果是,我应该使用RTCPeerConnection.onconnectionstatechange
并考虑永久关闭连接吗?RTCPeerConnection.connectionState
"closed"
"failed"
"disconnected"