Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript RTPeerConnection.createOffer“;“承诺”;使用_Javascript_Webrtc_Es6 Promise - Fatal编程技术网

Javascript RTPeerConnection.createOffer“;“承诺”;使用

Javascript RTPeerConnection.createOffer“;“承诺”;使用,javascript,webrtc,es6-promise,Javascript,Webrtc,Es6 Promise,我最近在学习WebRTC,在这里发现了“承诺”的用法() localConnection和removeConnection是RTPeerConnection对象。 从这里开始 createOffer: void createOffer(RTCSessionDescriptionCallback successCallback, RTPeerConnectionErrorCallback failureCallback,可选 媒体约束) createOffer有3个参数。但是为什么上面的代码没有参

我最近在学习WebRTC,在这里发现了“承诺”的用法()

localConnection和removeConnection是RTPeerConnection对象。 从这里开始

createOffer:

void createOffer(RTCSessionDescriptionCallback successCallback, RTPeerConnectionErrorCallback failureCallback,可选 媒体约束)

createOffer有3个参数。但是为什么上面的代码没有参数呢?参数在哪里?

旧的一个(一个在)有三个参数,可与firefox(包括最新版本)和chrome、es5兼容,这是一种基于旧回调的方法来检索值。(这是我正在使用的应用程序)

以下代码是较新的,适用于最新的firefox,不适用于最新的chrome:

localConnection.createOffer()
    .then(offer => localConnection.setLocalDescription(offer))
    .then(() => remoteConnection.setRemoteDescription(localConnection.localDescription))...
出于好奇,刚刚检查了混合时会发生什么,我想你已经知道了
。然后
=
成功回调
。catch
=
错误回调

localConnection.createOffer( offer => {
        console.log('in success callback', offer); 
        if(offer) localConnection.setLocalDescription(offer);
    }, error => {
        console.log('in error callback', error);             
    })
    .then(offer => {
        console.log('in promise then', offer); 
        if(offer) localConnection.setLocalDescription(offer);
    }).then(() => remoteConnection.setRemoteDescription(localConnection.localDescription))...
在chrome中:它将运行成功回调,同时抛出
未定义的
没有方法
的错误,然后


在firefox中:它将运行successcallback,并使用值
undefined
解析

因为API已经通过承诺实现了现代化,而文档已经过时

之前:

pc.createOffer(onSuccess, onFailure, options);
pc.createOffer(options).then(onSuccess, onFailure)

pc.createOffer(onSuccess, onFailure, options);
pc.createOffer(options).then(onSuccess, onFailure)
其中,
选项
是可选的。多亏了polyfill,这应该可以在所有支持WebRTC的浏览器中使用(在Firefox中也可以本机使用)

ES6
=>
箭头功能在Firefox和Chrome 45中工作,您可以在其中尝试:

var pc1=new RTCPeerConnection(),pc2=new RTCPeerConnection();
var add=(pc,can)=>can&&pc.addIceCandidate(can).catch(失败);
pc1.onicecandidate=e=>add(pc2,e.candidate);
pc2.onicecandidate=e=>add(pc1,e.candidate);
pc2.onaddstream=e=>v2.srcObject=e.stream;
pc1.oniceconnectionstatechange=e=>log(pc1.iceConnectionState);
var start=()=>
navigator.mediaDevices.getUserMedia({video:true,audio:true})
.then(stream=>pc1.addStream(v1.srcObject=stream))
。然后(()=>pc1.createOffer())。然后(d=>pc1.setLocalDescription(d))
.then(()=>pc2.setRemoteDescription(pc1.localDescription))
。然后(()=>pc2.createAnswer())。然后(d=>pc2.setLocalDescription(d))
.then(()=>pc1.setRemoteDescription(pc2.localDescription))
.捕获(失败);
var log=msg=>div.innerHTML+=“”+msg+“

”; var failed=e=>log(e+,行“+e.lineNumber)


开始
Include可在Chrome中使用承诺版的WebRTC API。代码非常有用,谢谢。adapter.js的URL也应替换为
https://webrtc.github.io/adapter/adapter-latest.js
在小提琴中。