javascript - 无法设置本地应答 sdp : Called in wrong state: STATE_INPROGRESS

标签 javascript node.js websocket webrtc rtcdatachannel

我有两个客户:

1) Windows 7 PC 上的 Chrome(版本 50.0.2661.102 m)
2) Android 平板电脑上的 Chrome(版本 50.0.2661.89)

两者都在同一个网络中(因此不需要 STUN/TURN 服务器)。

我在装有 Centos 6 的 VirtualBox VM 上使用我自己的信号服务器,该服务器是用 node.js (webSocket) 构建的。

客户端之间的视频/声音通信工作正常。现在我想将文件从一个客户端传输到另一个客户端。作为我的代码的基础,我使用这个例子的代码 here

正如这段代码所建议的,我在创建 PeerConnection 之后创建了 dataChannnel。

function createPeerConnection() {
....
  myPeerConnection = new RTCPeerConnection(iceServers, optional);        
  myDataChannel = myPeerConnection.createDataChannel('myDataChannel');

  // Set up event handlers for the ICE negotiation process.
  myPeerConnection.onicecandidate = handleICECandidateEvent;
  myPeerConnection.onaddstream = handleAddStreamEvent;
  myPeerConnection.onnremovestream = handleRemoveStreamEvent;
  myPeerConnection.oniceconnectionstatechange =               handleICEConnectionStateChangeEvent;
  myPeerConnection.onicegatheringstatechange = handleICEGatheringStateChangeEvent;
  myPeerConnection.onsignalingstatechange = handleSignalingStateChangeEvent;
  myPeerConnection.onnegotiationneeded = handleNegotiationNeededEvent;
  myPeerConnection.ondatachannel = handleDataChannel;

  myDataChannel.onmessage = handleDataChannelMessage;
  myDataChannel.onopen = handleDataChannelOpen;
}
...
... 
function invite(peerId) {
   ...
   createPeerConnection();
   ...
}
...
...
function handleVideoOfferMsg(msg) {
   thereIsNegotiation = true;
   targetUsername = msg.name;

   // Call createPeerConnection() to create the RTCPeerConnection.
   log("Starting to accept invitation from " + targetUsername);
   createPeerConnection();

   // We need to set the remote description to the received SDP offer
   // so that our local WebRTC layer knows how to talk to the caller.
   var desc = new RTCSessionDescription(msg.sdp);

   myPeerConnection.setRemoteDescription(desc)
   .then(function(stream) {
      log("-- Calling myPeerConnection.addStream()");

      return myPeerConnection.addStream(localStream);
   })
   .then(function() {
      log("------> Creating answer");
      // Now that we've successfully set the remote description, we need to
      // start our stream up locally then create an SDP answer. This SDP
      // data describes the local end of our call, including the codec
      // information, options agreed upon, and so forth.
     return myPeerConnection.createAnswer();
   })
   .then(function(answer) {
      log("------> Setting local description after creating answer");
      // We now have our answer, so establish that as the local description.
      // This actually configures our end of the call to match the settings
      // specified in the SDP.
      return myPeerConnection.setLocalDescription(answer);
   })
   .then(function() {
      var msg = {
         name: clientId,
         room: roomId,
         target: targetUsername,
         type: "video-answer",
         sdp: myPeerConnection.localDescription
      };
    // We've configured our end of the call now. Time to send our
    // answer back to the caller so they know that we want to talk
    // and how to talk to us.
    log("Sending answer packet back to other peer");

    sendToServer(msg);
   })
   .catch(handleGetUserMediaError);
}

当第二个客户提出报价时,第一个客户试图做出回答时,我得到了错误

打开相机和/或麦克风时出错:无法设置本地应答 spd: Failed to push down transport 描述: 提供本地指纹 但没有可用的身份。

打开相机和/或麦克风时出错:无法设置本地应答 spd:在错误状态下调用:STATE_INPROGRESS

只有一次创建成功。

我必须在其他地方创建 DataChannel 吗?喜欢这里:

function handleICEConnectionStateChangeEvent {

   switch(myPeerConnection.iceConnectionState) {
      ...
      case "connected":       
         createDataChannel();
         break;           
   }
}

function createDataChannel(){
    myDataChannel = myPeerConnection.createDataChannel('myDataChannel');
    myPeerConnection.ondatachannel = handleDataChannel;

    myDataChannel.onmessage = handleDataChannelMessage;
    myDataChannel.onopen = handleDataChannelOpen;      
}

有什么建议吗?

最佳答案

此代码中的错误是发送方和接收方都创建了新的数据通道。正确的做法是,创建数据通道

myDataChannel = myPeerConnection.createDataChannel('myDataChannel') 

另一个等待数据通道的创建:

myPeerConnection.ondatachannel = handleDataChannel;

关于javascript - 无法设置本地应答 sdp : Called in wrong state: STATE_INPROGRESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37412992/

相关文章:

javascript - 'separate domain sandboxing'(防止 JS cookie 劫持)是否仍然适用于子域?

node.js - 从AWS lambdas访问kubernetes集群中的Mongo副本

javascript - 如何从接口(interface)中的API读取JSON文件

java - Spring Boot 端点 + webSocket

websocket - 通过 Websocket 发送 WebRTC MediaStream(RTP over HTTP/Websocket)

javascript - 禁用输入字段填充 1 个字符时自动跳转到下一个输入字段

javascript - 事件处理程序返回 true J​​avascript jQuery

javascript - 使用效果无限循环,即使状态数据没有改变

javascript - 语音 channel 不设防

swift - SocketIOClient : Handling event: error with data: ["Invalid SSL certificate"]