我什么时候可以考虑断开 RTCPeerConnection?
When can I consider a RTCPeerConnection to be disconnected?
我正在尝试检测 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 的以下内容让我停下来,它可能不会在生产中保持:
Of course, "disconnected" and "closed" don't necessarily indicate errors; these can be the result of normal ICE negotiation, so be sure to handle these properly (if at all).
如果 RTCPeerConnection.connectionState
是 "closed"
、"failed"
或 "disconnected"
,我是否应该改用 RTCPeerConnection.onconnectionstatechange
并考虑永久关闭连接?
规范针对该主题提供了精心设计的建议:
Performing an ICE restart is recommended when iceConnectionState
transitions to "failed"
. An application may additionally choose to listen for the iceConnectionState
transition to "disconnected"
and then use other sources of information (such as using getStats
to measure if the number of bytes sent or received over the next couple of seconds increases) to determine whether an ICE restart is advisable.
见the spec and the PR that added this
请注意,由于错误 Chrome 在统一计划中不再转到“失败”。 “关闭”只有在您的代码调用 pc.close() 时才会发生,因此从 Chrome 80 起不再在 iceconnectionstatechange 中触发。
我正在尝试检测 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 的以下内容让我停下来,它可能不会在生产中保持:
Of course, "disconnected" and "closed" don't necessarily indicate errors; these can be the result of normal ICE negotiation, so be sure to handle these properly (if at all).
如果 RTCPeerConnection.connectionState
是 "closed"
、"failed"
或 "disconnected"
,我是否应该改用 RTCPeerConnection.onconnectionstatechange
并考虑永久关闭连接?
规范针对该主题提供了精心设计的建议:
Performing an ICE restart is recommended when
iceConnectionState
transitions to"failed"
. An application may additionally choose to listen for theiceConnectionState
transition to"disconnected"
and then use other sources of information (such as usinggetStats
to measure if the number of bytes sent or received over the next couple of seconds increases) to determine whether an ICE restart is advisable.
见the spec and the PR that added this
请注意,由于错误 Chrome 在统一计划中不再转到“失败”。 “关闭”只有在您的代码调用 pc.close() 时才会发生,因此从 Chrome 80 起不再在 iceconnectionstatechange 中触发。