setLocalDescription(); を呼び出すまで、PeerConnection は候補の収集を開始しません。setLocalDescription に提供される情報は、収集する必要がある候補の数を PeerConnection に通知します。( setLocalDescription のこの動作は、https://datatracker.ietf.org/doc/html/draft-ietf-rtcweb-jsep-03#section-4.2.4の定義に示されています)
同じブラウザ ウィンドウで 2 つの PeerConnections 間の接続を確立するための完全なフローは次のようになります (シグナリングに焦点を当てるために省略された MediaStreams の追加)。
var pc1, pc2, offer, answer;
pc1 = new webkitRTCPeerConnection(options);
pc2 = new webkitRTCPeerConnection(options);
pc1.onicecandidate = function(candidate) {
pc2.addIceCandidate(candidate);
};
pc2.onicecandidate = function(candidate) {
pc1.addIceCandidate(candidate);
};
pc1.createOffer(onOfferCreated, onError);
function onError(err) {
window.alert(err.message);
}
function onOfferCreated(description) {
offer = description;
pc1.setLocalDescription(offer, onPc1LocalDescriptionSet, onError);
}
function onPc1LocalDescriptionSet() {
// after this function returns, pc1 will start firing icecandidate events
pc2.setRemoteDescription(offer, 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');
}
質問に mozPeerConnection が含まれているため、Firefox は現在「トリクル候補」を生成していないことに注意してください。これは、候補アドレスがオファー/アンサーに「c」行として含まれ、onicecandidate コールバックが呼び出されないことを意味します。
このアプローチの欠点は、オファー/アンサーを作成する前に、すべての候補が収集されるまで Firefox が待機する必要があることです (このプロセスには、STUN および TURN サーバーへの接続と、応答または要求のタイムアウトのいずれかの待機が含まれる可能性があります)。