1
votes

i've tried every webrtc demo out there. none of them can connect everytime. and this is just a localhost connection. i do not think this is my fault. i've had a friend try it too, usually it works, but if you retry the demo enough eventually it will fail to connect.

the issue is that pc.connectionState == "failed"

is webrtc actually unable to connect consistently? how is nobody complaining about this?

here's one of the better demos i've tried. https://scaledrone.github.io/webrtc-text-chat-tutorial/index.html source code is here https://github.com/ScaleDrone/webrtc-text-chat-tutorial

i challenge you to try it. connect to yourself. close both tabs and try again about 10 times. it should get stuck connecting. is there anyway to fix it? i've been messing with the code for all demos for weeks but can't make anything connect robustly!

the only fix i found is that calling pc.addIceCandidate before pc.remoteDescription.type exists is sometimes a race condition problem, fixing that seemed to help, but connections still get stuck

i tried with the latest chrome and firefox. i'm pretty sure when i first tried webrtc 3 years ago it connected 100% of the time.

if you have any webrtc code that can connect every time, post it as an answer, thanks!

1

1 Answers

3
votes

Even though using it on localhost, you'll need iceServers. It's because, you're using datachannel. And it may fail whenever ice server cannot establish the connection. And it can also be due to offer collision during the process. In that case, you can try re-negotiating the peers:

In onconnectionstatechange event: (For offer collision)

if (pc.connectionState === 'failed') {
  pc.restartIce()
}

In oniceconnectionstatechange event: (For ice server)

if (pc.iceconnectionState === 'failed') {
  pc.restartIce()
}

This will let the peer retry connecting through negotiation: onnegotiationneeded event.