WebRTC 丢失连接后重新连接(重新连接尝试)

发布于 2025-01-11 05:27:25 字数 774 浏览 1 评论 0原文

我有一个可用的 WebRTC JavaScript 应用程序。问题是:如果在 Web 通话期间网络连接不良,则通话会停止,而 WebRTC 不会尝试重新连接。

我想通过添加自动重新连接尝试系统来改进我的应用程序的代码,但是,为了做到这一点,我需要了解一些有关 WebRTC 的理论(我认为这对于许多其他开发人员来说非常有用)。

以下是我的问题:

  1. 如果网络不好,WebRTC 是否具有尝试重新连接的本机功能,或者我应该监听一些“断开连接触发程序”并调用“函数”以从零开始新连接?

  2. 如果事情不能神奇地完成,那么正确的“断开连接触发器”和重新连接尝试过程应该重新启动的“函数”是什么?是否可以(或应该)从先前的连接中获取某些内容?

  3. 我已阅读有关 {iceRestart: true} 参数的信息。这应该用于第一次调用(然后 WebRTC 将通过尝试重新连接来神奇地处理断开连接),还是应该仅在我的代码尝试重新连接时使用它(第二次、第三次...)?

  4. connectionState“已断开”、“失败”和“已关闭”之间有什么区别?它与尝试在网络状况不佳时重新连接有关吗?

  5. 如果没有希望重新连接(即:互联网完全关闭),避免在无限循环中尝试重新连接的最佳方法是什么?

  6. oniceconnectionstatechange 和 onconnectionstatechange 有什么区别?哪个与我的情况相关?

谢谢!

卢卡

I have a working WebRTC JavaScript application. Here is the problem: if during a web call there is a bad network connection, the call is stopped without WebRTC attempting to reconnect.

I would like to improve the code of my application by adding an automatic reconnection attempt system, however, in order to do so I need to understand some theory about WebRTC (and I think this can be very useful for many other developers).

Here are my questions:

  1. Does WebRTC have a native functionality to attempt reconnection if the network is bad or should I listen for some "disconnection tigger" and call "a function" to start a new connection from zero?

  2. If things cannot be done magically, what is/are the right "disconnection tigger/s" and "the function" from which the reconnection attempt process should restart? Is there something that can (or should) be taken from the previous connection?

  3. I have read about an {iceRestart: true} parameter. Should this be used for the first call (then WebRTC will magically handle disconnection by attempting to reconnect) or should I use it ONLY when my code attempts to reconnect (on the 2nd, 3rd times...)?

  4. What is the difference between connectionState "disconnected", "failed" and "closed" and does it have something to do with attempting to reconnect with bad network?

  5. What is the best way to avoid attempting to reconnect in an infinite loop if there is no hope to reconnect (i.e: internet completely down)?

  6. What is the difference between oniceconnectionstatechange and onconnectionstatechange? which is relevant in my case?

Thanks!

Luca

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-01-18 05:27:25

我能够通过实验找到(JavaScript)解决方案...

1)WebRTC 是否具有本机功能,可以在网络不好时尝试重新连接,或者我应该监听一些“断开连接触发程序”并调用“函数”从零开始一个新的连接?

是的,它在 JavaScript 中默认执行此操作,除非您的代码通过附加指令行主动终止调用来处理断开连接。

2)如果事情不能神奇地完成,那么正确的“断开连接触发器”和重新连接尝试过程应该重新启动的“功能”是什么?是否可以(或应该)从之前的连接中获取某些内容?

事情已经在幕后发生了(通过魔法)。如果代码终止呼叫,可能是因为断开连接触发器(ICE 连接状态 = 断开连接或连接状态 = 断开连接)触发了您从某处复制/粘贴的应用程序中的一些附加代码。

3) 我已阅读有关 {iceRestart: true} 参数的内容。这是否应该用于第一次调用(然后 WebRTC 将通过尝试重新连接来神奇地处理断开连接),或者我应该仅在我的代码尝试重新连接时使用它(第二次、第三次......)?

无用在这种情况下。

4)connectionState“已断开”、“失败”和“已关闭”之间有什么区别?它与尝试在网络状况不佳时重新连接有关吗?

您必须监听connectionState =已断开连接,其他的对于这个目的来说并不重要。

5) 如果没有希望重新连接(即:互联网完全关闭),避免尝试在无限循环中重新连接的最佳方法是什么?

没问题,WebRTC 自动处理的重新连接不会花费任何费用因此,就信令而言,您可以根据需要多次尝试重新连接,如果速度太慢,用户最终将自行退出通话。

6) oniceconnectionstatechange 和 onconnectionstatechange 有什么区别?这与我的情况相关?

在这种情况下没有区别,唯一的区别是冰状态变化是在连接状态变化之前触发的。

——

我希望这对某人有帮助!

I was able to find the (JavaScript) solution through experimenting...

1) Does WebRTC have a native functionality to attempt reconnection if the network is bad or should I listen for some "disconnection tigger" and call "a function" to start a new connection from zero?

Yes, it does it by default in JavaScript, unless your code handles disconnection by proactively terminating the call through additional lines of instructions.

2) If things cannot be done magically, what is/are the right "disconnection tigger/s" and "the function" from which the reconnection attempt process should restart? Is there something that can (or should) be taken from the previous connection?

Things already happen under the hood (by magic). If the code terminates the call, it is probably because the disconnection trigger (ICE connection state = disconnected OR connection state = disconnected) triggers some additional code from the app you copy/pasted from somewhere.

3) I have read about an {iceRestart: true} parameter. Should this be used for the first call (then WebRTC will magically handle disconnection by attempting to reconnect) or should I use it ONLY when my code attempts to reconnect (on the 2nd, 3rd times...)?

Not useful in this scenario.

4) What is the difference between connectionState "disconnected", "failed" and "closed" and does it have something to do with attempting to reconnect with bad network?

You have to listen for connectionState = disconnected, the other ones don't matter for this purpose.

5) What is the best way to avoid attempting to reconnect in an infinite loop if there is no hope to reconnect (i.e: internet completely down)?

No problem, the reconnection WebRTC handles automatically will not cost anything in terms of signaling, therefore, you can try to reconnect as many times as you want, the user will eventually exit the call on his own if things are too slow.

6) What is the difference between oniceconnectionstatechange and onconnectionstatechange? which is relevant in my case?

No difference in this case, the only difference is that the ice state change is triggered right before the connection state change.

--

I hope this will be helpful to somebody!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文