javascript - 如何开始收集 ICE 候选人以进行对等连接

标签 javascript webrtc real-time p2p

我正在开发两个对等点之间的信号系统,并注意到 RTCPeerConnection.onicecandidate 事件没有触发。我检查了 iceGatheringState,它总是返回"new",这意味着对等连接尚未开始搜索 ice 候选对象。

我如何从本地机器开始收集 ice 候选对象以发送给对等机?

如果我不想让候选人流失,我如何才能在收集到他们后通过 sdp 发送他们?

这是我当前的代码,我能够成功获取 sdp 数据并捕获它们以进行发送,因此 ice 和检查两个客户端是否已连接是唯一的问题。

var peerConn = new webkitRTCPeerConnection(
    {'iceServers':[{'url':'stun:stun.1.google.com:19302'}]}
);
var remoteConn = new webkitRTCPeerConnection(
    {'iceServers':[{'url':'stun:stun.1.google.com:19302'}]}
);

alert(peerConn.iceGatheringState);

///Event Handlers//
//will be called when each event occurs

//onicecandidate
//returns local ice candidates (when gathered) to be sent to peer
//peerConn.onicecandidate = onicecandidate;
peerConn.onicecandidate = function(iceEvent){ //not firing
    if(iceEvent.candidate === null){
        alert(peerConn.iceConnectionState);
        alert(iceEvent.candidate);

        //send to peer or put in with sdp data
    }
}

最佳答案

一旦您使用通过 createOffer 或 createAnswer 生成的 SDP 调用 setLocalDescription,ICE 收集就会开始。

如果您不想使用 trickle ice,请等待空候选,然后发送 peerConn.localDescription.sdp 的内容——然后应该包括候选。

关于javascript - 如何开始收集 ICE 候选人以进行对等连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533288/

相关文章:

c++ - 实时 TCP/IP 堆栈

javascript - 请帮助测试 401 时 Firefox jQuery ajax 中的 CORS 问题

javascript - 如何设置:before psudo property for a group of css styles

reactjs - simpleWebRTC RemoteMedia 视频不包含正确的 screenCapture bool

javascript - WebRTC 硬编码连接(无服务器)

opengl - 有人可以描述 Ken Silverman 的 Voxlap 引擎使用的算法吗?

real-time - CODESYS 中众多功能 block 的实例化 (IEC61131)

javascript - 切换不适用于显示备用消息

javascript - 当鼠标悬停在固定位置 div 上时滚动

webrtc - 我们可以将 google stun 服务器(nodejs webrtc.io-client 的默认设置)用于商业应用程序吗?