我什么时候可以考虑断开 RTCPeerConnection? [英] When can I consider a RTCPeerConnection to be disconnected?

查看:62
本文介绍了我什么时候可以考虑断开 RTCPeerConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检测 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 重启.应用程序还可以选择侦听 iceConnectionStatedisconnected" 的转换,然后使用其他信息源(例如使用 getStats测量在接下来的几秒钟内发送或接收的字节数是否增加)以确定是否建议重新启动 ICE.

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.

请参阅规范添加这个的公关

请注意,由于存在错误,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆