15

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 ?

mido
  • 24,198
  • 15
  • 92
  • 117
Jeremy Li
  • 337
  • 1
  • 3
  • 10
  • 1
    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 `connected` – mido Aug 18 '15 at 03:50
  • @mido22 It works like a charm !! : ) – Jeremy Li Aug 21 '15 at 06:17
  • 1
    hii @JeremyLee may i know how to reconnect after iceConnectionState.name() is "closed". – Vivek Patel Sep 11 '17 at 12:10

2 Answers2

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 !!!

Jeremy Li
  • 337
  • 1
  • 3
  • 10
  • 10
    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. – Harminder Singh Oct 09 '17 at 07:55
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.

mehdi
  • 1
  • 2