我的 webrtc 网络应用程序从未启动
onicecandidate()
。
当我查看本地(Webrtc 对象)对象时,设置了 localdescription
和 remotedescription
。
在 Firefox 中,它会触发 onicecandidate()
但 event.candidate
为空。
我尝试了几个小时来解决这个问题,但我做不到
有人能解决这个问题吗?
如何才能触发
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 之前。