RTCPeerConnection.createAnswer回调在Mozilla中返回未定义的对象以进行WebRTC聊天 [英] RTCPeerConnection.createAnswer callback returns undefined object in mozilla for WebRTC chat

查看:383
本文介绍了RTCPeerConnection.createAnswer回调在Mozilla中返回未定义的对象以进行WebRTC聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following is my code to answer the incoming call:

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回调获得的answerundefined.结果,它给了我以下错误:

The above code works fine for chrome, but when i run the same in mozilla, the answer obtained from pc.createAnswer callback is undefined. As a result of which it gives me following error:

TypeError:RTCPeerConnection.setLocalDescription的参数1不是 一个对象.

TypeError: Argument 1 of RTCPeerConnection.setLocalDescription is not an object.

推荐答案

问题是您没有检查错误,特别是:没有传递必需的错误回调.

The problem is you're not checking errors, specifically: not passing in the required error callbacks.

setRemoteDescriptionsetRemoteDescription需要三个参数 (旧版回调样式)或一个(是),但是您再传两个.对于createAnswer减一也是一样.

setRemoteDescription and setRemoteDescription require either three arguments (legacy callback style) or one (promises), but you're passing in two. Same for createAnswer minus one.

浏览器的JS绑定最终选择了错误的重载,向您返回了一个您也不检查的承诺,有效地吞噬了错误.

The browser's JS bindings end up picking the wrong overload, returning you a promise which you're not checking either, effectively swallowing errors.

添加必要的错误回调:

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:

Or use the modern 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中的 adapter.js 可用.参见小提琴.

The promise API is available natively in Firefox, or through adapter.js in Chrome. See fiddle.

并始终检查错误. ;)

And always check for errors. ;)

这篇关于RTCPeerConnection.createAnswer回调在Mozilla中返回未定义的对象以进行WebRTC聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆