feross / simple-peer

📡 Simple WebRTC video, voice, and data channels
MIT License
7.39k stars 971 forks source link

Can't get a connection using the sample code #653

Closed squareparticle closed 4 years ago

squareparticle commented 4 years ago

I am new to WebRTC so I am just trying to get the sample code in the README.md to connect.

Below is the sample code I cut and pasted into an empty html file and posted it to my website. I do know to use #1 on the first page and not to use it on the second page.

<html>
    <body>
        <style>
        #outgoing {
            width: 600px;
            word-wrap: break-word;
            white-space: normal;
        }
        </style>
        <form>
        <textarea id="incoming"></textarea>
        <button type="submit">submit</button>
        </form>
        <pre id="outgoing"></pre>
        <script src="simplepeer.min.js"></script>
        <script>
        const p = new SimplePeer({
            initiator: location.hash === '#1',
            trickle: false
        })

        p.on('error', err => console.log('error', err))

        p.on('signal', data => {
            console.log('SIGNAL', JSON.stringify(data))
            document.querySelector('#outgoing').textContent = JSON.stringify(data)
        })

        document.querySelector('form').addEventListener('submit', ev => {
            ev.preventDefault()
            p.signal(JSON.parse(document.querySelector('#incoming').value))
        })

        p.on('connect', () => {
            console.log('CONNECT')
            p.send('whatever' + Math.random())
        })

        p.on('data', data => {
            console.log('data: ' + data)
        })
        </script>
    </body>
</html>

When I try to run it on 2 computers on my home network by cutting an pasting the offer and the answer from computer to computer I get the error below...

error Error: Connection failed.
    at a (simplepeer.min.js:6)
    at p._onConnectionStateChange (simplepeer.min.js:6)
    at RTCPeerConnection._pc.onconnectionstatechange (simplepeer.min.js:6)

I can get a connection sometimes when I go from tab to tab on the same computer (but not always?)

What am I doing wrong? I have tried running the above html on my desktop, on an http site and on an https site with no difference. I downloaded simplepeer.min.js from this repository today.

nazar-pc commented 4 years ago

It should work on the same machine (even different browsers) all the time, unless browser is configured in a weird way (like extensions that block WebRTC functionality). If you have different computers, especially over Internet, you may need to use a TURN server.

I do not think there is an issue in simple-peer, you can get that generic even with plain RTCPeerConnection. Closing, but feel free to continue conversation.

squareparticle commented 4 years ago

I just needed help with ideas on why I can't connect. I am not trying to submit a bug. I just can't get it to run for an unknown reason and I have nobody else to turn to when asking about how to get your sample code to connect...

Is there a way to expand on that error to show me why it can't connect?

I have not tried to run it across computers outside of my house yet. Only internally as I have a few computers but I can't get any of them to make a successful connection to each other yet.

What port is the connection using? Could it be my router or antivirus blocking anything?

As I said, I am new to this. I am not sure how or when to use a TURN server. I thought WebRTC was peer-peer after making a connection and I thought cutting and pasting the offer and the answer was bypassing the need for an external connection server?

Unfortunately, I did get a few connection errors from tab to tab. However, it usually does work.

squareparticle commented 4 years ago

I switched browsers to Firefox so that I would have no extensions running and now the error as changed to

"Ice connection failed."

Does that mean simple-peer is trying to connect to an Ice server for me and for whatever reason it can't?

nazar-pc commented 4 years ago

If you just quickly search though questions here on GitHub, there are many with "Ice connection failed" and some good suggestions as to why this may be. We are primarily here to maintain and improve simple-peer and not doing generic WebRTC support, because there is a lot of demand for that and very few of us here reviewing issues in our spare time.

Does that mean simple-peer is trying to connect to an Ice server for me and for whatever reason it can't?

It is connection error from WebRTC stack in the browser, not simple-peer itself as such. It is a generic error saying that ICE connection failed between two peers, meaning there was no way for peers to make direct connection through firewalls and such or it failed in the middle of the process for some reason.

What port is the connection using? Could it be my router or antivirus blocking anything?

You can find that in standard dev tools in your Firefox (about:webrtc) or Chromium-based browser (chrome://webrtc-internals), everything is in there. It can easily be, of course.

I thought WebRTC was peer-peer after making a connection and I thought cutting and pasting the offer and the answer was bypassing the need for an external connection server?

It is, but you still need a server for signaling in most cases and it is not always possible to establish P2P connection.

Please do some research first, there many good sources online about WebRTC and it is not as simple as it may seem in real world.

squareparticle commented 4 years ago

Thanks for your help, I spent the entire weekend trying to get simple-peer to connect and I didn't understand the lines between what is WebRTC and what simple-peer was doing. I just found out that WebRTC even existed last week. I thought it would go great with my companies web offering. We sell K-8 Math/Reading programs to schools and it would be nice to have a way for teachers to chat with students while they are working from home.

Anyways, I did find a solution to my connection problem. I have switched to using PeerJS and it just works? I have no idea what the difference is but PeerJS works on all my devices in my home network?? I didn't change my router, antivirus, or my browser plugins nor did I start using a TURN server, so I can't tell you why PeerJS works...

nazar-pc commented 4 years ago

Then there should have been a bug in a previous signaling code.

squareparticle commented 4 years ago

Possibly, but you saw the code I was using was just a cut and paste from this github page? And I was using the latest simplepeer.min.js. So I am confused...

nazar-pc commented 4 years ago

Time is also important, SDPs only live about 30 seconds. Also for STUN timing is also quite important for successful completion. There are more reasons for things to go wrong rather than right :slightly_smiling_face:

squareparticle commented 4 years ago

It is quite possible that it was taking me longer than 30 seconds to email myself the offer and another email for the answer back. Whereas PeerJS's sample code only requires me to email just the offer and it somehow makes a connection right away. Thanks for your help, I will keep trying.