WebRTC, STUN/TURN not working outside LAN

2019-07-05 00:09发布

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.

1条回答
仙女界的扛把子
2楼-- · 2019-07-05 00:39

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.

查看更多
登录 后发表回答