Web RTC 连接未建立
Web RTC connection is not established
当我运行此代码连接未建立时,
在 firefox 控制台报错“WebRTC: ICE failed, add a STUN server and
有关更多详细信息,请参阅 about:webrtc”,并且 dataChannel.readyState 是
正在连接
in Chrome console error does not show up but again
dataChannel.readyState 正在连接
var localconn, remoteconn, dataChannel, recievedDataChannel;
connect();
async function connect(){
localconn = new RTCPeerConnection();
remoteconn = new RTCPeerConnection();
dataChannel = localconn.createDataChannel("dataChannel");
dataChannel.onopen = e =>{
console.log("data channel is open");
}
dataChannel.onmessage = e=>{
console.log("new message: " +e.data);
}
remoteconn.ondatachannel = e =>{
recievedDataChannel = e.channel;
recievedDataChannel.onmessage = e=>{
console.log("new message from remote : " +e.data);
}
}
var offer = await localconn.createOffer();
await localconn.setLocalDescription(offer);
await remoteconn.setRemoteDescription(offer);
var answer = await remoteconn.createAnswer();
await remoteconn.setLocalDescription(answer);
await localconn.setRemoteDescription(answer);
}
您需要一个用于对等连接上的 ICE 候选者的侦听器,并将其应用于另一个对等点。
localconn.onicecandidate = function(event) {
if (event.candidate) {
remoteconn.addIceCandidate(event.candidate);
} else {
// All ICE candidates have been sent
}
}
remoteconn.onicecandidate = function(event) {
if (event.candidate) {
localconn.addIceCandidate(event.candidate);
} else {
// All ICE candidates have been sent
}
}
另见 https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/onicecandidate
当我运行此代码连接未建立时,
在 firefox 控制台报错“WebRTC: ICE failed, add a STUN server and 有关更多详细信息,请参阅 about:webrtc”,并且 dataChannel.readyState 是 正在连接
in Chrome console error does not show up but again dataChannel.readyState 正在连接
var localconn, remoteconn, dataChannel, recievedDataChannel; connect(); async function connect(){ localconn = new RTCPeerConnection(); remoteconn = new RTCPeerConnection(); dataChannel = localconn.createDataChannel("dataChannel"); dataChannel.onopen = e =>{ console.log("data channel is open"); } dataChannel.onmessage = e=>{ console.log("new message: " +e.data); } remoteconn.ondatachannel = e =>{ recievedDataChannel = e.channel; recievedDataChannel.onmessage = e=>{ console.log("new message from remote : " +e.data); } } var offer = await localconn.createOffer(); await localconn.setLocalDescription(offer); await remoteconn.setRemoteDescription(offer); var answer = await remoteconn.createAnswer(); await remoteconn.setLocalDescription(answer); await localconn.setRemoteDescription(answer);
}
您需要一个用于对等连接上的 ICE 候选者的侦听器,并将其应用于另一个对等点。
localconn.onicecandidate = function(event) {
if (event.candidate) {
remoteconn.addIceCandidate(event.candidate);
} else {
// All ICE candidates have been sent
}
}
remoteconn.onicecandidate = function(event) {
if (event.candidate) {
localconn.addIceCandidate(event.candidate);
} else {
// All ICE candidates have been sent
}
}
另见 https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/onicecandidate