javascript - Firefox 中的 WebRTC ICE 候选者

标签 javascript google-chrome firefox webrtc rtcdatachannel

目前,我正在研究 WebRTC。我的目标是在两个浏览器之间设置数据通道。 Chrome-Chrome 运行良好。现在我正在玩Firefox-Firefox。这是我当前代码中的 MEW:

var servers = { "iceServers": [{ "url": "stun:stun.l.google.com:19302" }] };
var RTCPeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
var IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;

var peerConnection = new RTCPeerConnection(servers, { optional: [{ RtpDataChannels: true }] });
peerConnection.onicecandidate = function (event) {
    peerConnection.onicecandidate = null;
    console.log('ICE Candidate:', JSON.stringify(event.candidate))
};

var channel = peerConnection.createDataChannel("sendDataChannel", {reliable: false});

peerConnection.createOffer(
    function (offer) {
        peerConnection.setLocalDescription(offer);
    }, function (e) { }
);

一旦调用setLocalDescription,就会调用函数onicecandidate(如预期)。在 Chrome 36 中,event.icecandidate 类似于:

{"sdpMLineIndex":0,"sdpMid":"audio","candidate":"a=candidate:3430859439 1 udp 2122260223 xxx.xxx.xxx.xxx 59773 typ host generation 0\r\n"} 

在 Firefox 中,event.icecandidate 只是 null。但我需要通过信令 channel 发送 ICE 候选者以建立连接。

最佳答案

Chrome 会触发 onicecandidate 事件,因为它支持“trickle-ice”。另一方面,Firefox 不支持trickle-ice。如果您注意到 Firefox 生成的 SDP,它应该已经包含必要的候选行。 null onicecandidate 事件表示冰收集已完成(对于 Chrome 和 Firefox),您可以将 SDP 发送给对等方。

关于javascript - Firefox 中的 WebRTC ICE 候选者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488318/

相关文章:

javascript - 如何为库设置 TypeScript 编译器,以便 Webpack 在依赖项目中切断未使用的模块?

ruby - 在 Watir Webdriver 中使用当前保存的 Cookie

firefox - 在 Firefox 和 Chrome 中查看来自 WCF 数据服务的原始 XML

jquery - 根据所选选项动态更改选择元素的背景颜色

javascript - 类型错误 : Cannot use 'in' operator to search for 'SUPPORT_WL_SERVER_CHANGE' in null

php - 简单的安全登录 php/javascript

javascript - 在 cordova 应用程序中使用相机拍摄照片

javascript - Simpel 图像切换功能不起作用(没有 jQuery)

javascript - Facebook 不会触发刷新事件,Tampermonkey 也无法工作?

html - Google Chrome 中链接的垂直边距