简体   繁体   English

Webrtc和socket.io:createanswer()总是有错误。 为什么?。 我的代码将两个对等点连接起来,但是其中只有一个知道吗?

[英]Webrtc and socket.io: createanswer() always has an error. Why?. My code connects two peers, but only one of them knows about it?

I am using socket.io and serving on node.js. 我正在使用socket.io并在node.js上提供服务。

Here is code from the client side: 这是来自客户端的代码:

getUserMedia(constraints, handlemedia, error);
function handlemedia(stream) {
  localStream = stream;
  attachMediaStream(localVideo, stream);
  socket = io.connect();

  document.getElementById("connect").addEventListener("click", function(e){
        remoteVideo = document.getElementById("remoteVideo");
        console.log("We know what socket is.", socket);
        servers = {"iceServers": [{ "url": "stun:stun.l.google.com:19302"}]};
       pc = new RTCPeerConnection(servers);
       pc.onicecandidate = console.log("GOT CANDIDATE");
       pc.addStream(localStream);

       pc.onaddstream = function(event) 
       {
           attachMediaStream(remoteVideo, event.stream);
       }

       function error()
       {
           console.log("An error occurred in the create offer section. ");
       }
       sdpconstraints = {'mandatory': {
       'OfferToReceiveVideo':true }};


        pc.createOffer(function(sessiondesc) 
        {
        console.log("Socket attemp 2: ", socket);
        pc.setLocalDescription(sessiondesc);
        console.log("Offer: ", sessiondesc);
        console.log("local description was set. Don't worry.");
        socket.emit("sessiondesc", sessiondesc);
        }, error, sdpconstraints);


        socket.on("sessiondesc", function(sessiondesc)
         {
        console.log("WE GOT THE FUNCTION ");
        sdpconstraints = {'mandatory': {
        'OfferToReceiveVideo':true }};


        console.log("Your partner created an offer for you.");

            pc.createAnswer(function(sessiondesc) 
            {
                console.log("Socket attemp at create answer section: ", socket);
                pc.setLocalDescription(sessiondesc);
                console.log("Session description:  ", sessiondesc);
                console.log("Local desc for create answer section was set.");
                socket.emit("sessiondesc", sessiondesc);
            }, console.log("Error in create answer section occurred."), sdpconstraints);

         });
  }

}

And here is the server code, using node.js and socket.io: 这是使用node.js和socket.io的服务器代码:

var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket, webkitRTCPeerConnection,     client, id){

    socket.on("sessiondesc", function(sessiondesc)
    {
        console.log("Emitting session desc RIGHT NOW");
        socket.broadcast.emit("sessiondesc", sessiondesc);
    });


 });

So what happens is I open up two tabs in my browser, go to localhost on both tabs, and make sure I have console open to see the logs. 因此,发生的事情是我在浏览器中打开了两个选项卡,在两个选项卡上都转到了localhost,并确保已打开控制台以查看日志。

Then I click connect on the first tab. 然后,单击第一个选项卡上的连接。 It displays logs up to "local description set dont worry", but it does not display "WE GOT THE FUNCTION" stuff, which makes sense, because I haven't connected on the other tab yet. 它显示的日志最多为“本地描述集不用担心”,但不显示“ WE GOT THE FUNCTION”,这是有道理的,因为我尚未在另一个选项卡上进行连接。

So I go to the other tab, and click connect. 因此,我转到另一个选项卡,然后单击“连接”。 Now, on this second tab it only displays the stuff up to the local description set don't worry. 现在,在第二个选项卡上,它仅显示不超过本地描述集的内容。 So obviously the second tab isn't receiving anything from the first tab! 因此,显然第二个选项卡没有从第一个选项卡接收任何信息! Why? 为什么?

BUT when I go back to the first tab, it did receive something! 但是当我回到第一个选项卡时,它确实收到了一些东西! It went through the "We got the function" part of my code. 它经过了代码的“我们得到了功能”部分。 Then it does say "your partner created an offer for you" BUT the problem is, the next thing it says is "error in create answer section", so there was an error in createanswer! 然后,它确实说“您的合作伙伴为您创建了一个要约”,但问题是,接下来的问题是“创建答案部分出错”,因此createanswer中存在错误! Why is this? 为什么是这样? Why did it call this error? 为什么称此错误?

It also doesn't display any remote videos on any of the tabs. 它还不会在任何选项卡上显示任何远程视频。

I have been able to identify couple of problems with your code... 我已经能够识别出您的代码中的几个问题...

  • you handle the sessiondesc from socket only after the person clicks on the connect button, so when tab1 emits that, tab2 does not catch it. 您仅在该人单击connect按钮sessiondesc从套接字处理sessiondesc ,因此,当tab1发出该信号时,tab2不会捕获它。
  • Second, when tab2 sends offer and tab1 tries to add it, it's peerConnection has already created offer, so cannot create answer( this part is my assumption that PeerConnection can either only create offer or create answer. ) 其次,当tab2发送要约并且tab1尝试添加要约时,它的peerConnection已经创建了要约,因此无法创建要约(这是我的假设,PeerConnection只能创建要约或创建要约。)

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

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