RTPeerConnection.createAnswer回调返回mozilla中用于WebRTC聊天的未定义对象

RTPeerConnection.createAnswer回调返回mozilla中用于WebRTC聊天的未定义对象,webrtc,mozilla,Webrtc,Mozilla,以下是我接听来电的代码: var pc = connection.pc; pc.setRemoteDescription(sdp,function() { pc.createAnswer(function(answer) { pc.setLocalDescription(answer,function() { // code for sending the answer }) }) }) 上面的代码对chrome很有效,但当我在mozilla中运行相同的代码时,从pc.cre

以下是我接听来电的代码:

var pc = connection.pc;
pc.setRemoteDescription(sdp,function() {
 pc.createAnswer(function(answer) {
  pc.setLocalDescription(answer,function() {
   // code for sending the answer
 }) 
 })
})
上面的代码对chrome很有效,但当我在mozilla中运行相同的代码时,从
pc.createAnswer
回调中获得的
answer
undefined
。因此,它给了我以下错误:

TypeError:RTCPeerConnection.setLocalDescription的参数1不正确 物体


问题是您没有检查错误,特别是:没有传入所需的错误回调

setRemoteDescription
setRemoteDescription
需要(传统回调样式)或(承诺),但您要传递两个。对于
createAnswer
减去一,相同

浏览器的JS绑定最终选择了错误的重载,并向您返回了一个承诺,而您也没有检查该承诺,从而有效地抑制了错误

添加必要的错误回调:

var pc = connection.pc;
pc.setRemoteDescription(sdp, function() {
  pc.createAnswer(function(answer) {
    pc.setLocalDescription(answer, function() {
      // code for sending the answer
    }, function(e) {
      console.error(e);
    });
  }, function(e) {
    console.error(e);
  });
}, function(e) {
  console.error(e);
});
或者使用现代promise API:

var pc = connection.pc;
pc.setRemoteDescription(sdp)
  .then(() => pc.createAnswer())
  .then(answer => pc.setLocalDescription(answer))
  .then(() => {
    // code for sending the answer
  })
  .catch(e => console.error(e));
promise API在Firefox中本机提供,或通过Chrome提供。看

并始终检查错误