简体   繁体   English

WebRTC在本地工作,但不能在不同的IP地址上工作

[英]WebRTC works locally, but not across different IP addresses

My code is on Github at: https://github.com/rashadrussell/webrtc_experiment/blob/master/public/script.js 我的代码在Github上: https : //github.com/rashadrussell/webrtc_experiment/blob/master/public/script.js

I am trying to write a 1-to-1 video video conferencing script with WebRTC. 我正在尝试使用WebRTC编写一对一视频会议视频脚本。 And is being stored on AppFog, a cloud hosting website. 并存储在云托管网站AppFog上。 It works on my localhost when I test with two different Chrome windows on a single computer. 当我在一台计算机上使用两个不同的Chrome窗口进行测试时,它可以在本地主机上运行。 It also works on AppFog when I test it on two different computers at home. 当我在家中的两台不同计算机上对其进行测试时,它也可以在AppFog上运行。

The problem occurs when I test my app with a friend living at a different house. 当我与住在不同房子的朋友测试我的应用程序时,会出现问题。 The remote streams are not being set. 未设置远程流。 My only guess is that there is some error with IP addresses, which means something is wrong my the setup of Ice Candidates. 我唯一的猜测是IP地址存在错误,这意味着Ice Candidates的设置有问题。 All that pops up is a black box where the remote stream is supposed to be. 弹出的只是一个黑匣子,应该是远程流所在的地方。

Here is some of my code: 这是我的一些代码:

**Client-Side**

var isInitiator = false;


socket.on('initiatorFound', function(data) {
    isInitiator = data.setInitiator;
    console.log("Is Initiator? " + isInitiator);
});


navigator.getMedia = (
        navigator.getUserMedia ||
        navigator.webkitGetUserMedia ||
        navigator.mozGetUserMedia ||
        navigator.msGetUserMedia
    );

navigator.getMedia(
    {video: true, audio: false},
    function(stream) {
        var video = document.getElementById("localView");
        video.src = window.URL.createObjectURL(stream);
        console.log("Add Stream");
        sendMessage('streamAdd', {streamAdded: 'stream-added'});

        createPeerConnection();
        pc.addStream(stream);

        if(isInitiator)
        {
            callPeer();
        }

    },
    function(err) {
        console.log("The following error occured: ");
        console.dir(err);
    }

);


function sendMessage(type, message)
{
    console.log("Sending Message");
    socket.emit('message',{
        "type": type,
        "message": message
    });
}

function createPeerConnection() {

    pc = new rtcPeerConnection(servers, options);
    console.dir(pc);

    pc.onicecandidate = function(evt) {
        if(evt.candidate == null) return; 
        pc.onicecandidate = null;           

        console.log("Send Ice Candidate");
        sendMessage("iceCandidate", JSON.stringify(evt.candidate));
    };

    pc.onaddstream = function(evt) {
        document.body.append("<video id='remoteVideo' autoplay></video>");
        var remoteVid = document.getElementById("remoteVideo");
        remoteVid.src = window.URL.createObjectURL(evt.stream);
    };

}


function callPeer() {

    pc.createOffer(function (offer) {
            pc.setLocalDescription(offer, function() {
                sendMessage("offer", JSON.stringify(offer));
            });
            console.log("Send Offer");
        }, function(err) { console.log("Offer Error: " + err) },
            videoConstraints
        );

}

function answerPeer() {

    pc.createAnswer(function(answer) {
        pc.setLocalDescription(answer);
        sendMessage("answer", JSON.stringify(answer))
    }, function(err) { console.log("Sending Answer Error: " + err) },
        videoConstraints
    );

}

socket.on('message', function(message) {
    console.log("CONSOLE MESSAGE:");
    console.dir(message);

    if(message.type == 'streamAdd') {
        console.log('Stream was added');
        createPeerConnection();

        if(isInitiator) {
            callPeer();
        }

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

        pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message)));

        if(!isInitiator)
        {
            console.log("Sending Answer");
            answerPeer();
        }


    } else if(message.type == 'answer') {
        pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message)));
    } else if(message.type == 'iceCandidate') {
        console.log("Get Ice Candidate");
        pc.addIceCandidate(new rtcIceCandidate(JSON.parse(message.message)) );
    }

});

Server-Side 服务器端

var isInitiator = false;
io.sockets.on('connection', function(socket) {

    if (!isInitiator) {
      isInitiator = true;
      socket.emit('initiatorFound', {setInitiator: isInitiator});
    } else {
      socket.emit('initiatorFound', {setInitiator: !isInitiator});
    }

    // Signaling Channel
    socket.on('message', function(message) {

      if (message.type == 'streamAdd') {
        console.log('Got message: ' + message);
      }
      //socket.emit('message' ,message);
      // Should be:
      socket.broadcast.emit('message', message);

    });

});

In the line 在行中

pc = new rtcPeerConnection(servers, options); pc =新的rtcPeerConnection(服务器,选项);

While sending the server info through the server variable, you need to mention the addr of TURN servers as well along withthe STUN server. 通过server变量发送服务器信息时,您需要提及TURN服务器的地址和STUN服务器。 Typically the data gets blocked due to NAT or firewall and TURN should help in that case. 通常情况下,数据会由于NAT或防火墙而被阻止,在这种情况下,TURN应该会有所帮助。

For more server details, you can check out https://gist.github.com/yetithefoot/7592580 有关服务器的更多详细信息,可以查看https://gist.github.com/yetithefoot/7592580

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM