Justin Uberti posted this response to a StackOverflow question about how WebRTC connections work:
why doesn't "onicecandidate" work?
Now I've spent the better part of a week setting up a stun-turn server, and when it comes to setting the connections, they tell you to send the object as a string made from JSON.stringify(description)
Now for the life of me I cannot get this to work in his JS example, which I've changed here:
var pc1, pc2, offer, answer, offer_str, offer_prsd, answer_str, answer_prsd;
pc1 = new webkitRTCPeerConnection(getServerOptionsFromUsername("mr_boombosstik"));
pc2 = new webkitRTCPeerConnection(getServerOptionsFromUsername("countdracula"));
pc1.onicecandidate = function(candidate) {
pc2.addIceCandidate(candidate);
};
pc2.onicecandidate = function(candidate) {
pc1.addIceCandidate(candidate);
};
//PC1 KICKS OFF THE PROCESS WITH AN OFFER!!
pc1.createOffer(onOfferCreated, onError);
function onError(err) {
window.alert(err.message);
}
function onOfferCreated(description) {
offer = description;
//HERE IS THE OFFER CREATED!!!
pc1.setLocalDescription(offer, onPc1LocalDescriptionSet, onError);
offer_str = JSON.stringify(offer);
//WE SET IT AS "OUR LOCAL DESCRIPTIION
}
function onPc1LocalDescriptionSet() {
offer_prsd = JSON.parse(offer_str);
console.log(offer_prsd.sdp);
// after this function returns, pc1 will start firing icecandidate events
pc2.setRemoteDescription(offer_prsd, onPc2RemoteDescriptionSet, onError);
}
function onPc2RemoteDescriptionSet() {
pc2.createAnswer(onAnswerCreated, onError);
}
function onAnswerCreated(description) {
answer = description;
pc2.setLocalDescription(answer, onPc2LocalDescriptionSet, onError);
}
function onPc2LocalDescriptionSet() {
// after this function returns, you'll start getting icecandidate events on pc2
pc1.setRemoteDescription(answer, onPc1RemoteDescriptionSet, onError);
}
function onPc1RemoteDescriptionSet() {
window.alert('Yay, we finished signaling offers and answers');
}
So can anyone tell me why this won't work? If I can’t get the session description set locally, how am I going to get it done through a signalling service of some kind?