Why is my Ice Candidate request firing 6 times instead of 1?

Viewed 1591

I'm writing my first peer to peer connection application using WebRTC, and my code to request an ice candidate from the peer, which I send over a socket.io connection, is triggering 6 times rather than once.

This is really confusing because if I had mistakenly designed a big request loop, I would expect infinite recursions, not just 6 (8 onicecandidate events). So can anyone tell me why the follow code produced the 6 recursions?

Here's the message handler, it simply sends a socket.io message controlled by the syntax: Muveoo.Messenger.input('ice candidate request', data);

'ice candidate request' : function(data) {
    console.log('Debug 10: Requesting Ice Candidate');
    socket.emit('ice candidate request', data);
},

And here's the code that handles the Ice Candidate Request, dont be confused by the if logic at the very top, the UID is just a unique ID assigned to each client to decide who should make the offer initially.

if (Muveoo.RTC.connectedPeers[id].dataChannels[name].UID < Muveoo.RTC.connectedPeers[id].dataChannels[name].peerUID) {
    Muveoo.RTC.connectedPeers[id].dataChannels[name].offerConnection(function() {
        console.log('[Debug A]: Offering Connection');
        Muveoo.RTC.connectedPeers[id].dataChannels[name].pc.onicecandidate = function(evt) {
            console.log('[Debug A]: onicecandidate Event Triggered.');
            if (evt.candidate) {
                console.log('[Debug A]: Sending Ice Candidate Request.');
                Muveoo.Messenger.input('ice candidate request', {
                    target : id,
                    candidate : evt.candidate,
                    channel : name
                });
            }
        };
        Muveoo.RTC.connectedPeers[id].dataChannels[name].pc.ondatachannel = function(evt) {
            console.log('got data channel');
            Muveoo.RTC.connectedPeers[id].dataChannels[name] = evt.channel;
            Muveoo.RTC.connectedPeers[id].dataChannels[name].channel.onmessage = function(evt1) {
                handleMessage(evt1.data);

            };
            Muveoo.RTC.connectedPeers[id].dataChannels[name].channel.message = function(msg) {
                Muveoo.RTC.connectedPeers[id].dataChannels[name].channel.send(JSON.stringify(msg));
            };
        };
        socket.on('session description', function(data) {
            console.log('Debug 12: Session Description Received');
            Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].desc = new Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].sessionDescription(msg.desc);
            Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].pc.setRemoteDescription(Muveoo.RTC.connectedPeers[data.target].dataChannels[data.name].desc);
            if (Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].UID > Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].peerUID) {
                /*They sent the sessionDescription first, so need an answer*/
                Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].pc.createAnswer(function(answer) {
                    /*The answer is this side's local description*/
                    Muveoo.RTC.connectedPeers[data.target].dataChannels[data.channel].pc.setLocalDescription(answer);
                    var data = {
                        target : data.target,
                        description : answer,
                        channel : data.channel
                    };
                    socket.emit('session description', data);
                });
            }
        });
    });
}

And here's the resulting logs to show what's going on:

[Debug A]: Offering Connection
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:101 [Debug A]: Sending Ice Candidate Request.
messenger.js:91 Debug 10: Requesting Ice Candidate
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:101 [Debug A]: Sending Ice Candidate Request.
messenger.js:91 Debug 10: Requesting Ice Candidate
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:101 [Debug A]: Sending Ice Candidate Request.
messenger.js:91 Debug 10: Requesting Ice Candidate
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:101 [Debug A]: Sending Ice Candidate Request.
messenger.js:91 Debug 10: Requesting Ice Candidate
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:101 [Debug A]: Sending Ice Candidate Request.
messenger.js:91 Debug 10: Requesting Ice Candidate
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:94 [Debug A]: onicecandidate Event Triggered.
rtc.js:101 [Debug A]: Sending Ice Candidate Request.
messenger.js:91 Debug 10: Requesting Ice Candidate
rtc.js:94 [Debug A]: onicecandidate Event Triggered.

Why is my Ice Candidate request firing 6 times instead of 1?

2 Answers

I just set a limit to it. I think you're supposed to need only one ICE candidate to send to the other peer

var limit = 0;    
this.pc.onicecandidate = ({ candidate }) => {
  if (limit == 0) {
     log("candidate");
     limit = 1;
  } else {
     return;
  }
});
Related