WebRTC,STUN / TURN无法在LAN外工作

时间:2014-06-14 16:59:35

标签: javascript webrtc lan stun turn

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);
    });
});

我有另一个文件stream.html,它为 Answerer 做了类似的事情。

setChannelEvents函数上有通道onmessage的实现。

这在我家的局域网上完美运行。

我已经使用ngrok服务将其放在网上,以便在我的本地网络之外进行尝试,但是无法使用。

我正在使用谷歌浏览器(更新到最新版本,目前为35.0.1916.153)。

有什么东西我不见了吗?是否有一个可以尝试使用TURN的WebRTC的工作示例?

如果需要,我可以添加剩余代码以及candidatesdp的一些输出。

注意:当我启动ngrok时,套接字上的IP被修改。

1 个答案:

答案 0 :(得分:6)

XirSys家伙在这里。 [=

我不太确定你的错误是什么,除了它不起作用。如果错误只是视频不流动,您应该知道TURN不会起作用,因为您已经过了TURN的嵌入式凭据。使用XirSys时,您必须致电/getIceServers以获得新鲜的"与您的帐户关联的STUN和TURN服务器集。必须发出此POST请求,并且每次发起呼叫时,结果都会放入iceServers变量

为了快速了解我们的平台,我建议您阅读以下指南:

非常感谢您对我们的服务表示出兴趣,如果您有任何问题或意见,请与我们联系。