RTCPeerConnection: icegatheringstatechange 事件
當 RTCPeerConnection 物件上的 ICE 候選收集過程的狀態發生變化時,會向其 onicegatheringstatechange 事件處理程式傳送 icegatheringstatechange 事件。這表示連線的 iceGatheringState 屬性值已更改。
當 ICE 開始收集連線候選時,該值從 new 變為 gathering,表示收集連線候選配置的過程已開始。當值變為 complete 時,構成 RTCPeerConnection 的所有傳輸都已完成 ICE 候選的收集。
注意: 雖然您可以透過監聽 icegatheringstatechange 事件並檢查 iceGatheringState 的值是否為 complete 來確定 ICE 候選收集是否完成,但您也可以讓 icecandidate 事件的處理程式檢查其 candidate 屬性是否為 null。這也表示候選收集已完成。
此事件不可取消,也不會冒泡。
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("icegatheringstatechange", (event) => { })
onicegatheringstatechange = (event) => { }
事件型別
一個通用的 Event。
示例
此示例建立了一個 icegatheringstatechange 事件的事件處理程式。
js
pc.onicegatheringstatechange = (ev) => {
let connection = ev.target;
switch (connection.iceGatheringState) {
case "gathering":
/* collection of candidates has begun */
break;
case "complete":
/* collection of candidates is finished */
break;
}
};
同樣,您可以使用 addEventListener() 來為 icegatheringstatechange 事件新增監聽器。
js
pc.addEventListener("icegatheringstatechange", (ev) => {
let connection = ev.target;
switch (connection.iceGatheringState) {
case "gathering":
// collection of candidates has begun
break;
case "complete":
// collection of candidates is finished
break;
}
});
規範
| 規範 |
|---|
| WebRTC:瀏覽器中的即時通訊 # dom-rtcpeerconnection-onicegatheringstatechange |
瀏覽器相容性
載入中…