我什么时候可以考虑断开 RTCPeerConnection? [英] When can I consider a RTCPeerConnection to be disconnected?
问题描述
我试图检测 RTCPeerConnection 的另一端何时断开连接.目前我正在使用我的 RTCPeerConnection 对象执行以下操作:
I'm attempting to detect when the other side of a RTCPeerConnection has disconnected. Currently I'm doing the following with my RTCPeerConnection object:
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 让我停顿一下,它可能不会在生产中坚持下去:
This seems to work in my limited testing with very simply network conditions but the following from MDN gives me pause that it's probably not going to hold up in production:
当然,断开连接"和关闭"并不一定表示错误;这些可能是正常 ICE 协商的结果,因此请务必妥善处理这些问题(如果有的话).
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.onconnectionstatechange
并考虑在 RTCPeerConnection.connectionState
为 "closed"
时连接永久关闭,失败"
还是 断开连接"
?
Should I instead be using RTCPeerConnection.onconnectionstatechange
and considering the connection permanently closed if RTCPeerConnection.connectionState
is "closed"
, "failed"
or "disconnected"
?
推荐答案
该规范针对该主题提出了非常精心的建议:
The specification has very carefully crafted advice on that topic:
当 iceConnectionState
转换为 failed"
时,建议执行 ICE 重启.应用程序还可以选择侦听 iceConnectionState
到 disconnected"
的转换,然后使用其他信息源(例如使用 getStats
测量在接下来的几秒钟内发送或接收的字节数是否增加)以确定是否建议重新启动 ICE.
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.
请注意,由于存在错误,Chrome 不再处于失败"状态.在统一计划中.关闭"仅当您的代码调用 pc.close() 时才会发生,因此自 Chrome 80 起不再在 iceconnectionstatechange 中触发.
Mind you that due to bugs Chrome no longer goes to "failed" in unified plan. "closed" can only happen if your code calls pc.close() so is no longer fired in iceconnectionstatechange as of Chrome 80.
这篇关于我什么时候可以考虑断开 RTCPeerConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!