sgoenka1
sgoenka1

Reputation: 53

Creating Peer Connection for Web RTC Data Channel

I have been trying to establish peer connection between browsers, to use data channel, but i am unsuccessful. Everytime I correct one statement another error appears.

First I established a socketting server using socket.io and Node.js. In the server when any client is connection I am sending 'beacon' packets. On listening 'beacon' packet 1st client requests to join a 'room'. Then I allow the second client to join the same 'room'. As soon as the second client connects, Server sends a confirmation packet to Client 1.

Then Client 1 sends the RTC Peer Connection 'offer' to Client 2, after setting local Description.

    if( isChrome )          {
        localPC = new window.webkitRTCPeerConnection(server, contraints);
        rslt.innerHTML = "Webkit Variables Set";
    }else       {
        localPC = new mozRTCPeerConnection(server, contraints); 
        rslt.innerHTML = "Mozilla Variables Set";
    }

    localPC.onicecandidate = function(event)    {
        if( event.candidate )
            localPC.addIceCandidate( event.candidate );
    };

    localPC.onnegotiationneeded = function()        {
        localPC.createOffer( setOffer, sendFail );
    };  

    sendChannel = localPC.createDataChannel( "sendDataChannel", {reliable: false} );

    localPC.ondatachannel = function(event)     {
        receiveChannel = event.channel;
        receiveChannel.onmessage = function(event)  {
            rslt.innerHTML = event.data;
        };
    };

    localPC.createOffer( setOffer, sendFail );



function setOffer( offer )      {

    lDescp = new RTCSessionDescription(offer);

    localPC.setLocalDescription( lDescp );

    socket.emit( 'offer', JSON.stringify(offer) );

    rslt.innerHTML += "Offer Sent...<br/>";//+offer.sdp;                                    

}//End Of setOffer()    

Client 2 on receiving the 'offer' sets its as remote Description and creates a 'reply'. Sets the 'reply' as local Description, and sends it.

                if( message.type == 'offer' )       {

                    rDescp = new RTCSessionDescription(message.sdp);
                    localPC.setRemoteDescription( rDescp );

                    localPC.createAnswer( 
                        function( answer )  {

                            lDescp = new RTCSessionDescription(answer);
                            localPC.setLocalDescription( lDescp );

                            socket.emit( 'reply', JSON.stringify(answer) );

                        }, sendFail
                    );                      

                }else   {

                    localPC.addIceCandidate = new RTCIceCandidate( message.candidate );

                }//End Of IF ELse

Client 1 on receiving the 'reply' sets it as remote Description and the connection should get established???

    localPC.setRemoteDescription( new RTCSessionDescription( message.sdp ) );

But its not working!!! Pleease Help.

Upvotes: 0

Views: 864

Answers (1)

shacharz
shacharz

Reputation: 213

Seems like you got the flow correct, although I don't see the entire code. One thing that strikes me weird is this:

localPC.onicecandidate = function(event)    {
        if( event.candidate )
            localPC.addIceCandidate( event.candidate );
};

You need to send the icecandidate recieved in the onicecandidate event to the other peer. and not add it yourself.

Upvotes: 2

Related Questions