gpt4 book ai didi

javascript - 我什么时候可以考虑断开 RTCPeerConnection?

转载 作者:行者123 更新时间:2023-12-02 16:46:41 28 4
gpt4 key购买 nike

我正在尝试检测 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",我是否应该改用 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 specthe PR that added this

请注意,由于错误,Chrome 不再在统一计划中“失败”。 “关闭”只有在您的代码调用 pc.close() 时才会发生,因此从 Chrome 80 开始不再在 iceconnectionstatechange 中触发。

关于javascript - 我什么时候可以考虑断开 RTCPeerConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60229785/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com