目前,我正在研究 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/