How can WebRTC reconnect to the same peer after disconnection?
Asked Answered
G

3

17

Now I am working on a peer-to-peer chatting system based on WebRTC. This system can make a pair with any person who is listening on the peer list at the same time and I have finished the basic functionanity of real-time communication in audio and video. But I have no ideas how to reconnect to the same peer if it disconnected accidentally ?

Gales answered 17/8, 2015 at 10:11 Comment(3)
one way to do it might be listening to the PeerConnection's iceConnectionState, and when it changes to closed, start a process of automatically reconnecting the peer. One thing to note is, if iceConnectionState is disconnected, there is a change that it automatically changes back to connectedClack
@mido22 It works like a charm !! : )Gales
hii @JeremyLee may i know how to reconnect after iceConnectionState.name() is "closed".Plated
G
5

Thanks ! As mido22 mention that iceConnectionState automatically changes to connected if disconnected by some connection problem. I found some articles on here https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState , and it solved my confusion about the recovery operation of automatic-reconnecting to the same peer on some flaky network !!!

Gales answered 21/8, 2015 at 6:15 Comment(1)
hey I am stuck in this. how you achieve the reconnection. I am Already listening to it iceConnectionState . Yesterday it was automatically shift to connect state within 5 sec. (either from disconnect/ failed state -> connected) Today it is just moved to failed state from disconnect.Interloper
G
0

Put additional constraint:

1: cons.mandatory.add(new MediaConstraints.KeyValuePair("IceRestart", "true"));

Generate sdp file:

2: pc.createOffer(new WebRtcObserver(callbacks), cons);

Set resulted sdp to PeerConnection:

3: pc.setLocalDescription(new WebRtcObserver(callbacks), sdp);

4: Send it to remote peer.

So steps 2-4 are the same as for regular offer.

Globin answered 3/6, 2020 at 9:10 Comment(0)
H
0

Add event listener to iceconnectionstatechange event, and execute restartIce call.

e.g.

pc.addEventListener("iceconnectionstatechange", (event) => {
  if (pc.iceConnectionState === "failed" || pc.iceConnectionState === "disconnected") {
    /* possibly reconfigure the connection in some way here */
    /* then request ICE restart */
    pc.restartIce();
  }
});

Here is a MDN reference for more details

Hypnotherapy answered 16/7 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.