我有一个基本的webRTC应用程序,该应用程序支持两个对等方之间的视频/音频通信和文件共享。当我在Mozilla Firefox上打开该应用程序时,该应用程序按预期运行,但是当我在Google Chrome上运行它时, onicecandidate 返回null
我的RTCPeerConnection
myConnection = new RTCPeerConnection();
设置对等连接
myConnection.createOffer().then(offer => {
currentoffer = offer
myConnection.setLocalDescription(offer);
})
.then(function () {
myConnection.onicecandidate = function (event) {
console.log(event.candidate);
if (event.candidate) {
send({
type: "candidate",
candidate: event.candidate
});
}
};
send({
type: "offer",
offer: currentoffer
});
})
.catch(function (reason) {
alert("Problem with creating offer. " + reason);
});
在Mozilla Firefox上,您可以在控制台日志中看到在每个“ onicecandidate”事件中收集的所有ICE候选者
在Chrome上,输出为null
答案 0 :(得分:1)
创建对等连接时,您必须传递STUN / TURN服务器。
否则,您将仅本地候选人,因此只能在本地进行联系
var STUN = {
'url': 'stun:stun.l.google.com:19302',
};
var iceServers =
{
iceServers: [STUN]
};
var pc = new RTCPeerConnection(iceServers);
答案 1 :(得分:0)
在调用createOffer()
方法时,您应该传递选项对象,例如:
myConnection = new RTCPeerConnection();
var mediaConstraints = {
'offerToReceiveAudio': true,
'offerToReceiveVideo': true
};
myConnection.createOffer(mediaConstraints).then(offer => {
currentoffer = offer
myConnection.setLocalDescription(offer);
})
...// the rest of you code goes here
或者,您可以在创建要约之前指定RTCRtpTransceiver
:
myConnection = new RTCPeerConnection();
myConnection.addTransceiver("audio");
myConnection.addTransceiver("video");
myConnection.createOffer().then(offer => {
currentoffer = offer
myConnection.setLocalDescription(offer);
})
...// the rest of you code goes here
来源:
WebRTC 1.0
MDN RTCPeerConnection.createOffer()
MDN RTCPeerConnection.addTransceiver()
示例-GitHub