为什么不“onicecandidate”工作? [英] why doesn't "onicecandidate" work?

查看:630
本文介绍了为什么不“onicecandidate”工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过它的PeerConnection和'onicecandidate'事件了解webRTC。

I'm having trouble understanding webRTC with it's PeerConnection and 'onicecandidate' event.

据我所知,你必须使用STUN(或TURN)服务器启动对等连接,因为它会将你的ICE发回给你与另一个同行沟通的候选人。

As far as I understand it you must initiate a peerconnection using a STUN (or TURN) server, because it will send you back your ICE candidate for communication with another peer.

我已经看到了将PeerConnection对象的服务器参数输出的示例,我也不明白,但是我们只是说它确实需要服务器参数。

I've seen examples leaving the server parameter of the PeerConnection object out, which I don't understand as well, but let's just say it does need the server parameter.

所以,当我写下以下代码时:

So, when I write down the following code:

    var pc, ice = { "iceServers": [{ "url": "stun:stun.l.google.com:19302" }] };
if(typeof mozRTCPeerConnection === 'function') {

    pc = new mozRTCPeerConnection(ice);
}
else {
    console.log('google');
    pc = new webkitRTCPeerConnection(ice);
}


pc.onicecandidate  = function(event) { 
    console.log(event);
}

我希望'onicecandidate'事件会触发,但它不会工作。我也尝试过其他公共STUN服务器,但没有任何反应。所以我假设我的理解可能有些问题:)

I expect that the 'onicecandidate' event will fire, but it doesn't work. I tried other public STUN servers as well, but nothing happens. So I assume there is probably something wrong with my comprehension :)

推荐答案

在你调用setLocalDescription之前,PeerConnection不会开始收集候选人();提供给setLocalDescription的信息告诉PeerConnection需要收集多少候选人。 (setLocalDescription的此行为在其的定义中指出。 http://tools.ietf.org/html/draft-ietf-rtcweb-jsep-03#section-4.2.4

The PeerConnection won't start gathering candidates until you call setLocalDescription(); the information supplied to setLocalDescription tells PeerConnection how many candidates need to be gathered. (This behavior for setLocalDescription is indicated in its definition at http://tools.ietf.org/html/draft-ietf-rtcweb-jsep-03#section-4.2.4)

这是什么完整流程看起来像是在同一个浏览器窗口中建立两个PeerConnections之间的连接(添加MediaStreams而忽略了信号):

Here's what a complete flow looks like for establishing a connection between two PeerConnections in the same browser window (adding of MediaStreams omitted to focus on the signaling):

var pc1, pc2, offer, answer;

pc1 = new webkitRTCPeerConnection(options);
pc2 = new webkitRTCPeerConnection(options);

pc1.onicecandidate = function(candidate) {
  pc2.addIceCandidate(candidate);
};

pc2.onicecandidate = function(candidate) {
  pc1.addIceCandidate(candidate);
};

pc1.createOffer(onOfferCreated, onError);

function onError(err) {
  window.alert(err.message);
}

function onOfferCreated(description) {
  offer = description;
  pc1.setLocalDescription(offer, onPc1LocalDescriptionSet, onError);
}

function onPc1LocalDescriptionSet() {
  // after this function returns, pc1 will start firing icecandidate events
  pc2.setRemoteDescription(offer, onPc2RemoteDescriptionSet, onError);
}

function onPc2RemoteDescriptionSet() {
  pc2.createAnswer(onAnswerCreated, onError);
}

function onAnswerCreated(description) {
  answer = description;
  pc2.setLocalDescription(answer, onPc2LocalDescriptionSet, onError);
}

function onPc2LocalDescriptionSet() {
  // after this function returns, you'll start getting icecandidate events on pc2
  pc1.setRemoteDescription(answer, onPc1RemoteDescriptionSet, onError);
}

function onPc1RemoteDescriptionSet() {
  window.alert('Yay, we finished signaling offers and answers');
}

由于您在问题中包含了mozPeerConnection,我会注意到Firefox确实存在目前还没有产生'涓流候选人'。这意味着它将在候选/答案中将其候选地址包含为c行,并且永远不会调用onicecandidate回调。

Since you included a mozPeerConnection in your question, I'll note that Firefox does not currently generate 'trickle candidates'. This means that it will include its candidate addresses as 'c' lines in the offer/answer, and the onicecandidate callback will never be called.

这种方法的缺点是Firefox必须等待所有候选人在创建其提议/答案之前收集(这个过程可能涉及联系STUN和TURN服务器)并等待响应或请求超时)。

The downside to this approach is that Firefox must wait for all of its candidates to be gathered before creating its offer/answer (a process which can involve contacting STUN and TURN servers and waiting for either the responses or for the requests timeout).

这篇关于为什么不“onicecandidate”工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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