diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:52 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:52 -0500 |
commit | 074785cea106179cb3305637055ab0a009ca74f2 (patch) | |
tree | e6ae371cccd642aa2b67f39752a2cdf1fd4eb040 /files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html | |
parent | da78a9e329e272dedb2400b79a3bdeebff387d47 (diff) | |
download | translated-content-074785cea106179cb3305637055ab0a009ca74f2.tar.gz translated-content-074785cea106179cb3305637055ab0a009ca74f2.tar.bz2 translated-content-074785cea106179cb3305637055ab0a009ca74f2.zip |
initial commit
Diffstat (limited to 'files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html')
-rw-r--r-- | files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html b/files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html new file mode 100644 index 0000000000..f74735a97e --- /dev/null +++ b/files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html @@ -0,0 +1,141 @@ +--- +title: 'RTCPeerConnection: icecandidate event' +slug: Web/API/RTCPeerConnection/icecandidate_event +translation_of: Web/API/RTCPeerConnection/icecandidate_event +--- +<div>{{WebRTCSidebar}}</div> + +<p>Событие <strong><code>icecandidate</code></strong> отправляется {{domxref("RTCPeerConnection")}} когда {{domxref("RTCIceCandidate")}} был идентифицирован и добавлен к локальному клиенту (local peer) через вызов {{domxref("RTCPeerConnection.setLocalDescription()")}}. Обработчик события должен передать кандидата удаленному клиенту (remote peer) по каналу сигнализации (signaling channel), чтобы удаленный клиент (remote peer) смог добавить его в свой набор удаленных кандидатов (remote candidates).</p> + +<table class="properties"> + <tbody> + <tr> + <th scope="row">Всплывает</th> + <td>Нет</td> + </tr> + <tr> + <th scope="row">Отменяемое</th> + <td>Нет</td> + </tr> + <tr> + <th scope="row">Интерфейс</th> + <td>{{DOMxRef("RTCPeerConnectionIceEvent")}}</td> + </tr> + <tr> + <th scope="row">Название обработчика событий</th> + <td>{{DOMxRef("RTCPeerConnection.onicecandidate")}}</td> + </tr> + </tbody> +</table> + +<h2 id="Описание">Описание</h2> + +<p>Существует три причины, по которым событие <code>icecandidate</code> происходит (fired) у {{domxref("RTCPeerConnection")}}.</p> + +<h3 id="Делимся_Sharing_новым_кандидатом">Делимся (Sharing) новым кандидатом</h3> + +<p>В основном события <code>icecandidate</code> происходят, чтобы указать, что новый кандидат был построен (gathered). Этого кондидата нужно доставить удаленному клиенту (remote peer) через канал сигнализации (signaling channel), которым управляет ваш код.</p> + +<pre class="brush: js notranslate">rtcPeerConnection.onicecandidate = (event) => { + if (event.candidate) { + sendCandidateToRemotePeer(event.candidate) + } else { + /* there are no more candidates coming during this negotiation */ + } +} +</pre> + +<p>Удаленный клиент (peer), получив кандидата, добавит этого кандидата в свой пул кандидатов, используя вызов {{domxref("RTCPeerConnection.addIceCandidate", "addIceCandidate()")}}, передавая в {{domxref("RTCPeerConnectionIceEvent.candidate", "candidate")}} строку, которую вы передали с помощью сервера сигнализации (signaling server).</p> + +<h3 id="Indicating_the_end_of_a_generation_of_candidates">Indicating the end of a generation of candidates</h3> + +<p>When an ICE negotiation session runs out of candidates to propose for a given {{domxref("RTCIceTransport")}}, it has completed gathering for a <strong>generation</strong> of candidates. That this has occurred is indicated by an <code>icecandidate</code> event whose {{domxref("RTCPeerConnectionIceEvent.candidate", "candidate")}} string is empty (<code>""</code>).</p> + +<p>You should deliver this to the remote peer just like any standard candidate, as described under {{anch("Sharing a new candidate")}} above. This ensures that the remote peer is given the end-of-candidates notification as well. As you see in the code in the previous section, every candidate is sent to the other peer, including any that might have an empty candidate string. Only candidates for which the event's {{domxref("RTCPeerConnectionIceEvent.candidate", "candidate")}} property is <code>null</code> are not forwarded across the signaling connection.</p> + +<p>The end-of-candidates indication is described in <a href="https://tools.ietf.org/html/draft-ietf-mmusic-trickle-ice-02#section-9.3">section 9.3 of the Trickle ICE draft specification</a> (note that the section number is subject to change as the specification goes through repeated drafts).</p> + +<h3 id="Indicating_that_ICE_gathering_is_complete">Indicating that ICE gathering is complete</h3> + +<p>Once all ICE transports have finished gathering candidates and the value of the {{domxref("RTCPeerConnection")}} object's {{domxref("RTCPeerConnection.iceGatheringState", "iceGatheringState")}} has made the transition to <code>complete</code>, an <code>icecandidate</code> event is sent with the value of <code>complete</code> set to <code>null</code>.</p> + +<p>This signal exists for backward compatibility purposes and does <em>not</em> need to be delivered onward to the remote peer (which is why the code snippet above checks to see if <code>event.candidate</code> is <code>null</code> prior to sending the candidate along.</p> + +<p>If you need to perform any special actions when there are no further candidates expected, you're much better off watching the ICE gathering state by watching for {{domxref("RTCPeerConnection.icegatheringstatechange_event", "icegatheringstatechange")}} events:</p> + +<pre class="brush: js notranslate">pc.addEventListener("icegatheringstatechange", ev => { + switch(pc.iceGatheringState) { + case "new": + /* gathering is either just starting or has been reset */ + break; + case "gathering": + /* gathering has begun or is ongoing */ + break; + case "complete": + /* gathering has ended */ + break; + } +}); +</pre> + +<p>As you can see in this example, the <code>icegatheringstatechange</code> event lets you know when the value of the {{domxref("RTCPeerConnection")}} property {{domxref("RTCPeerConnection.iceGatheringState", "iceGatheringState")}} has been updated. If that value is now <code>complete</code>, you know that ICE gathering has just ended.</p> + +<p>This is a more reliable approach than looking at the individual ICE messages for one indicating that the ICE session is finished.</p> + +<h2 id="Examples">Examples</h2> + +<p>This example creates a simple handler for the <code>icecandidate</code> event that uses a function called <code>sendMessage()</code> to create and send a reply to the remote peer through the signaling server.</p> + +<p>First, an example using {{domxref("EventTarget.addEventListener", "addEventListener()")}}:</p> + +<pre class="brush: js notranslate">pc.addEventListener("icecandidate", ev => { + if (ev.candidate) { + sendMessage({ + type: "new-ice-candidate", + candidate: event.candidate + }); + } +}, false); +</pre> + +<p>You can also set the {{domxref("RTCPeerConnection.onicecandidate", "onicecandidate")}} event handler property directly:</p> + +<pre class="brush: js notranslate">pc.onicecandidate = ev => { + if (ev.candidate) { + sendMessage({ + type: "new-ice-candidate", + candidate: event.candidate + }); + } +}; +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{ SpecName('WebRTC 1.0', '#event-icecandidate', 'icecandidate') }}</td> + <td>{{Spec2('WebRTC 1.0')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.RTCPeerConnection.icecandidate_event")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/WebRTC_API">WebRTC API</a></li> + <li><a href="/en-US/docs/Web/API/WebRTC_API/Signaling_and_video_calling">Signaling and video calling</a></li> +</ul> |