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 ?
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 !!!
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.
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
© 2022 - 2024 — McMap. All rights reserved.
closed
, start a process of automatically reconnecting the peer. One thing to note is, if iceConnectionState isdisconnected
, there is a change that it automatically changes back toconnected
– Clack