3

index.html (Offerer)

var socket = io.connect('http://127.0.0.1:80'); //socket.io
socket.emit("player 1");

var iceServers = {
    iceServers: [
        {"url":"stun:turn1.xirsys.com"},
        {"username":"myusername","url":"turn:turn1.xirsys.com:443?transport=udp","credential":"mycredential"},
        {"username":"myusername","url":"turn:turn1.xirsys.com:443?transport=tcp","credential":"mycredential"}
    ]
};

var offererDataChannel, answererDataChannel;

var Offerer = {
    createOffer: function () {
        var peer = new PeerConnection(iceServers);
        var dataChannelOptions = {
            reliable: true,
            ordered: false
        };
        offererDataChannel = peer.createDataChannel('channel', dataChannelOptions);
        setChannelEvents(offererDataChannel);
        peer.onicecandidate = function (event) {
            if (event.candidate) {
            socket.emit("candidate", event.candidate);
        }
        };
        peer.createOffer(function (sdp) {
            peer.setLocalDescription(sdp);
            socket.emit("sdp", sdp);
        }, function (err) { peer.close(); });
        this.peer = peer;
        return this;
    },
    setRemoteDescription: function (sdp) {
        this.peer.setRemoteDescription(new SessionDescription(sdp));
    },
    addIceCandidate: function (candidate) {
        this.peer.addIceCandidate(new IceCandidate({
            sdpMLineIndex: candidate.sdpMLineIndex,
            candidate: candidate.candidate
        }));
    }
};

var Answerer = {
    createAnswer: function (offerSDP) {
        var peer = new PeerConnection(iceServers);
        peer.ondatachannel = function (event) {
            answererDataChannel = event.channel;
            setChannelEvents(answererDataChannel);
        };
        peer.onicecandidate = function (event) {
            if (event.candidate) {
                socket.emit("candidate", event.candidate);
            }
        };
        peer.setRemoteDescription(new SessionDescription(offerSDP));
        peer.createAnswer(function (sdp) {
            peer.setLocalDescription(sdp);
            socket.emit("sdp", sdp);
        }, function (err) { peer.close(); });
        this.peer = peer;
        return this;
    },
    addIceCandidate: function (candidate) {
        this.peer.addIceCandidate(new IceCandidate({
            sdpMLineIndex: candidate.sdpMLineIndex,
            candidate: candidate.candidate
        }));
    }
};

var peer = null;

socket.on("client is connected", function () {
    peer = Offerer.createOffer();
    socket.on("candidate", function (candidate) {
        peer.addIceCandidate(candidate);
    });
    socket.on("sdp", function (sdp) {
        peer.setRemoteDescription(sdp);
    });
});

I have another file stream.html that does a similar thing for the Answerer.

On the setChannelEvents function there is the implementation of the channel onmessage.

This works perfectly on my LAN at home.

I have used the ngrok service for put this online for trying outside my local network, and is not working.

I'm using Google Chrome (updated to the last version, currently 35.0.1916.153).

Is there something that I'm missing? Is there a working example of WebRTC with TURN that I can try?

If needed, I can add the remaining code plus some output of the candidate and sdp.

note: the IP on the socket is modified when I start ngrok.

genesisxyz
  • 778
  • 3
  • 14
  • 29
  • [Googles Apprtc example](https://apprtc.appspot.com/) using a TURN server for its connections...On your implementation, do you see the candidates being generated and being received by the other peer? – Benjamin Trent Jun 16 '14 at 13:15
  • @bwtrent yes, they get generated (RTCIceCandidate), 6 of them have my local IP (192.168.1.69) with port 56830, the last 3 (so, in total there are 9 candidates that are generated) have also the external IP, same port, but only for UDP – genesisxyz Jun 16 '14 at 13:58
  • You are getting candidates, so the turn server and ice process is working. The other thing to make sure of is that the other party is getting the candidates. Is this the case? – Benjamin Trent Jun 16 '14 at 14:05
  • @AdrienVinches, this is the [second WebRTC question](http://stackoverflow.com/questions/24239522/changing-a-mediastream-of-existing-rtcpeerconnection-webrtc/24268269#24268269) that I have seen you spamming with commercial-esque comments.... – Benjamin Trent Jun 18 '14 at 18:28
  • 1
    @bwtrent see answer below of the XirSys guy, the credential was wrong... my fault! Sorry... there is no problem in the code – genesisxyz Jun 18 '14 at 18:51

1 Answers1

6

XirSys guy here. [=

I'm not quite sure what your error is, other than it's just not working. If the error is simply that video isn't flowing, you should know that TURN won't work because you've embedded credentials for TURN that would have expired by now. When using XirSys, you have to call /getIceServers to get a "fresh" set of STUN and TURN servers that are associated with your account. This POST request must be made and the results placed into your iceServers variable each time you initiate a call.

In order to gain a quick understanding of our platform, I'd suggest reading the following guides:

Thanks so much for showing interest in our service, and please let me know if you have anymore questions or comments.

Benjamin Trent
  • 7,378
  • 3
  • 31
  • 41
onichase
  • 136
  • 4
  • 2
    I was doing it wrong, the credential was wrong... now is working perfectly (the only problem is Google Chrome that tries TURN first instead of STUN).. thanks! – genesisxyz Jun 18 '14 at 18:50