我的 webrtc 网络应用程序从来没有出现过 onicecandidate。 当我查看本地(Webrtc 对象)对象时,设置了本地描述和远程描述。 在 Firefox 中,它会触发 onicecandidate 但 event.candidate 为 null
我花了几个小时尝试解决这个问题,但没有成功
谁能解决这个问题吗?
如何触发 onicecandidate 事件?
let local = new RTCPeerConnection();
let remote = new RTCPeerConnection();
try {
local.createOffer().then(function(sdp) {
console.log("Offer Created by Local: " + sdp.sdp);
local.setLocalDescription(sdp);
remote.setRemoteDescription(sdp);
remote.createAnswer().then(function(sdp){
console.log("Answer Created by Remote: " + sdp.sdp);
remote.setLocalDescription(sdp);
local.setRemoteDescription(sdp);
local.onicecandidate = localicecandidate;
remote.onicecandidate = remoteicecandidate;
let channel = local.createDataChannel("channel");
channel.onopen = function(event) {
console.log("Channel opened");
}
channel.onmessgae = function(event) {
console.log(event.data);
}
remote.ondatachannel = function(event) {
let channel = event.channel;
channel.onopen = function(event) {
channel.send("Hi!");
}
}
function localicecandidate (event) {
console.log("Local got new Ice Candidate: " + event.candidate);
remote.addIceCandidate(new RTCIceCandidate(event.candidate));
}
function remoteicecandidate (event) {
console.log("Remote got new Ice Candidate: " + event);
local.addIceCandidate(new RTCIceCandidate(event.candidate));
}
});
})
}
catch (e) {
console.log("Got Error" + e);
}
最佳答案
在调用createOffer之前您没有调用createDataChannel,因此您的SDP不包含m=lines。如果没有 m 线,ICE 就没有任何谈判余地。
将 let channel = local.createDataChannel("channel");
移至 createOffer 之前。
关于javascript - WebRTC onicecandidate 永远不会在 Chrome 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311461/