WebRTC:连接交换显示黑色视频

时间:2017-01-11 17:53:25

标签: javascript node.js webrtc simplewebrtc

我正在尝试建立一个随机的连接系统。我有一个启动连接的按钮,还可以查找新的自动呼叫新对等体。但它是断断续续的,有时它完美无缺,有时候我不知道了。

后端 - server.js

    /** successful connection */
wss.on('connection', function (client) {
    console.log("A new WebSocket client was connected.");
    /** incomming message */
    client.on('message', function (message) {
      /** broadcast message to all clients */
        var obj = JSON.parse(message);
        if("callState" in obj) {
          // New client, add it to the id/client object
          // client.set('call_state') = 1
          console.log("Recebeu mensagem!!!");
        }else if("sdp" in obj || "ice" in obj) {
          wss.broadcast(message, client);
        }else{
          console.log("Recebeu: "+message);
        }
    });
});

// broadcasting the message to all WebSocket clients.
wss.broadcast = function (data, exclude) {
  console.log("Broadcasting message to all " + this.clients.length + " WebSocket clients.");
  for(var i in this.clients) {
    client = this.clients[i];
    // don't send the message to the sender...
    if (client === exclude) continue;
    if (client.readyState === client.OPEN) client.send(data);
    else console.error('Error: the client state is ' + client.readyState);
  }
};

前端 - webrtc.js

    /** button START */
function start(isCaller) {
    peerConnection = new RTCPeerConnection(peerConnectionConfig);
    peerConnection.onicecandidate = gotIceCandidate;
    peerConnection.addStream(localStream);

    if ('ontrack' in peerConnection) {
        // WebRTC Spec, Firefox
        peerConnection.ontrack = ontrack
     } else {
        // Chrome, etc. This can be removed once all browsers support `ontrack`
        peerConnection.onaddstream = gotRemoteStream
     }

    if(isCaller) {
        peerConnection.createOffer().then(createdDescription).catch(errorHandler);
    }
}

function gotMessageFromServer(message) {
    if(!peerConnection) start(false);

    var signal = JSON.parse(message.data);

    // Ignore messages from ourself
    if(signal.uuid == uuid) return;

    if(signal.sdp) {
        peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
            // Only create answers in response to offers
            if(signal.sdp.type == 'offer') {
                peerConnection.createAnswer().then(createdDescription).catch(errorHandler);
            }
        }).catch(errorHandler);
    } else if(signal.ice) {
        peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(errorHandler);
    }
}

在服务器日志中显示每次按“开始”按钮时,它会记录1条SDP消息和14条ICE消息

编辑:包含错误

当我第一次拨打“开始”按钮时,一切正常。但是,在以下呼叫中,有时仅保留音频功能,而在其他时间没有新连接。

  

几次单击“开始”后,我能够重现错误:   DOMException [InvalidStateError:“无法在状态中设置远程答案   稳定的“代码:11 nsresult:0x8053000b]

我正在运行本地服务器:https://luisdemarchi.aplicativo.info:8091

0 个答案:

没有答案