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 |
浏览器兼容性
加载中…