From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../icecandidate_event/index.html | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 files/ru/web/api/rtcpeerconnection/icecandidate_event/index.html (limited to 'files/ru/web/api/rtcpeerconnection/icecandidate_event') 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 +--- +
{{WebRTCSidebar}}
+ +

Событие icecandidate отправляется {{domxref("RTCPeerConnection")}} когда {{domxref("RTCIceCandidate")}} был идентифицирован и добавлен к локальному клиенту (local peer) через вызов {{domxref("RTCPeerConnection.setLocalDescription()")}}. Обработчик события должен передать кандидата удаленному клиенту (remote peer) по каналу сигнализации (signaling channel), чтобы удаленный клиент (remote peer) смог добавить его в свой набор удаленных кандидатов (remote candidates).

+ + + + + + + + + + + + + + + + + + + + +
ВсплываетНет
ОтменяемоеНет
Интерфейс{{DOMxRef("RTCPeerConnectionIceEvent")}}
Название обработчика событий{{DOMxRef("RTCPeerConnection.onicecandidate")}}
+ +

Описание

+ +

Существует три причины, по которым событие icecandidate происходит (fired) у {{domxref("RTCPeerConnection")}}.

+ +

Делимся (Sharing) новым кандидатом

+ +

В основном события icecandidate происходят, чтобы указать, что новый кандидат был построен (gathered). Этого кондидата нужно доставить удаленному клиенту (remote peer) через канал сигнализации (signaling channel), которым управляет ваш код.

+ +
rtcPeerConnection.onicecandidate = (event) => {
+  if (event.candidate) {
+    sendCandidateToRemotePeer(event.candidate)
+  } else {
+    /* there are no more candidates coming during this negotiation */
+  }
+}
+
+ +

Удаленный клиент (peer), получив кандидата, добавит этого кандидата в свой пул кандидатов, используя вызов {{domxref("RTCPeerConnection.addIceCandidate", "addIceCandidate()")}}, передавая в {{domxref("RTCPeerConnectionIceEvent.candidate", "candidate")}} строку, которую вы передали с помощью сервера сигнализации (signaling server).

+ +

Indicating the end of a generation of candidates

+ +

When an ICE negotiation session runs out of candidates to propose for a given {{domxref("RTCIceTransport")}}, it has completed gathering for a generation of candidates. That this has occurred is indicated by an icecandidate event whose {{domxref("RTCPeerConnectionIceEvent.candidate", "candidate")}} string is empty ("").

+ +

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 null are not forwarded across the signaling connection.

+ +

The end-of-candidates indication is described in section 9.3 of the Trickle ICE draft specification (note that the section number is subject to change as the specification goes through repeated drafts).

+ +

Indicating that ICE gathering is complete

+ +

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 complete, an icecandidate event is sent with the value of complete set to null.

+ +

This signal exists for backward compatibility purposes and does not need to be delivered onward to the remote peer (which is why the code snippet above checks to see if event.candidate is null prior to sending the candidate along.

+ +

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:

+ +
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;
+  }
+});
+
+ +

As you can see in this example, the icegatheringstatechange event lets you know when the value of the {{domxref("RTCPeerConnection")}} property {{domxref("RTCPeerConnection.iceGatheringState", "iceGatheringState")}} has been updated. If that value is now complete, you know that ICE gathering has just ended.

+ +

This is a more reliable approach than looking at the individual ICE messages for one indicating that the ICE session is finished.

+ +

Examples

+ +

This example creates a simple handler for the icecandidate event that uses a function called sendMessage() to create and send a reply to the remote peer through the signaling server.

+ +

First, an example using {{domxref("EventTarget.addEventListener", "addEventListener()")}}:

+ +
pc.addEventListener("icecandidate", ev => {
+  if (ev.candidate) {
+    sendMessage({
+      type: "new-ice-candidate",
+      candidate: event.candidate
+    });
+  }
+}, false);
+
+ +

You can also set the {{domxref("RTCPeerConnection.onicecandidate", "onicecandidate")}} event handler property directly:

+ +
pc.onicecandidate = ev => {
+  if (ev.candidate) {
+    sendMessage({
+      type: "new-ice-candidate",
+      candidate: event.candidate
+    });
+  }
+};
+
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{ SpecName('WebRTC 1.0', '#event-icecandidate', 'icecandidate') }}{{Spec2('WebRTC 1.0')}}
+ +

Browser compatibility

+ + + +

{{Compat("api.RTCPeerConnection.icecandidate_event")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf