I'm trying to understand how WebRTC works, mainly for using only DataChannel for game-networking experience. And this is what I've made so far. It gather ICE cadidates. I have two questions.
- Does offer need to be done for gathering ICE?
- Why
offerToReceiveAudio
orofferToReceiveVideo
needs to be set true, I'm gonna only useDatachannel
. (without one of this option set to true, ICE doesn't appears) (solved, see EDIT bellow)
Here goes a fiddle:
https://jsfiddle.net/t431a815/9/
and code:
var iceServers = [
]
var config = {
iceServers: iceServers,
iceTransportPolicy: "all",
rtcpMuxPolicy: 'negotiate'
};
var pcConstraints = {};
var offerOptions = {offerToReceiveAudio: true};
pcConstraints.optional = [{'googIPv6': true}]; // Whether we gather IPv6 candidates.
var pc = new RTCPeerConnection(config, pcConstraints);
pc.onicecandidate = iceCallback;
pc.createOffer(
offerOptions
).then(
gotDescription,
error
);
function gotDescription(desc) {
console.log("OFFER DESC:", desc);
pc.setLocalDescription(desc);
}
function error() {
console.log("sth goes wrong", arguments);
}
function iceCallback(event) {
console.log("ICE!", JSON.stringify(event.candidate));
}
EDIT:
found solution but it's weird, you just need to create one datachannel before making offer, then it works with offerToReceiveAudio: false, offerToReceiveVideo: false
var offererDataChannel = pc.createDataChannel('channel', {});
but why? What if I want to create it later?