Search code examples
javascriptwebrtc

Change RTCPeerConnection's change connectionState from disconnect to new


I want to change the connectionState manually. Consider the below scenario:

Step 1: P1 created a offer and sends to P2 - RTCPeerConnection state is new

Step 2: P2 generated answer and sends back to P1 - connection established and RTCPeerConnection state is stable

Step 3: P2 logged off. RTCPeerConnection state in P1 is disconnected

Step 4: P3 now tries to connect to P1. P3 generated the answer with the same offer (step 1, I stored it somewhere)

Now this Step 4 always fails. I know that I can create a new connection and create a new offer to connect with P4. But I don't want to do that. Is there any way to put the existing RTCPeerConnection at the initial state ?

Example Code:

myconnection = new RTCPeerConnection(configuration);
dataChannel = myconnection.createDataChannel("channel");
dataChannel.onclose = ()=>{
        console.log("data channel is closed");
        console.log(myconnection); //connectionState:disconnected; iceConnectionState: "disconnected"
        console.log(dataChannel); //readyState: "closed"
        
        //I Want to change the connectionState of myconnection here
      }

Solution

  • There is not because you're not supposed to use a peerconnection with more than a single peer. Destroy the current connection and make a new one.

    What might work is to call createOffer with {iceRestart: true} which signals the stack to reset some of the state. You're likely to run into bugs though.