我有一个基本的 webRTC 应用程序,它支持两个同行之间的视频/音频通信和文件共享,当我在 Mozilla Firefox 上打开它时,该应用程序按预期运行,但当我在 Google Chrome 上运行时,onicecandidate 返回空值
我的 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 上,输出为空
最佳答案
在调用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
关于javascript - 当我在 google chrome 浏览器上运行我的本地 webRTC 应用程序时,没有生成 ICE 候选人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572712/