RTCPeerConnectionStats
WebRTC API 中的 RTCPeerConnectionStats 字典提供了关于高级 RTCPeerConnection 的信息。
具体来说,它提供了已打开的唯一数据通道的数量,以及已关闭的通道数量。这使得可以计算出当前打开的通道数量。
可以通过迭代 RTCPeerConnection.getStats() 返回的 RTCStatsReport 来获取这些统计信息,直到找到一个 type 为 peer-connection 的报告。
实例属性
dataChannelsOpened-
一个正整数值,表示在其生命周期中进入
open状态的唯一RTCDataChannel对象的数量。 dataChannelsClosed-
一个正整数值,表示在其生命周期中离开
open状态的唯一RTCDataChannel对象的数量(未经open状态就转换到closing或closed状态的通道不计入此数量)。如果连接的任一端或底层传输关闭,通道将离开open状态。
常见实例属性
以下属性是所有 WebRTC 统计信息对象共有的。
id-
一个字符串,唯一标识正在监控以生成这组统计信息的对象。
时间戳-
一个
DOMHighResTimeStamp对象,表示此统计信息对象样本的采集时间。 type-
一个值为
"peer-connection"的字符串,表示对象包含的统计信息的类型。
示例
此示例展示了一个函数,用于返回打开的连接总数,如果未提供统计信息,则返回 null。此函数可能会在循环中调用,类似于 RTCPeerConnection.getStats() 示例 中的方法。
该函数等待对 RTCPeerConnection.getStats() 的调用结果,然后迭代返回的 RTCStatsReport 以仅获取类型为 "peer-connection" 的统计信息。然后,它使用报告中的数据返回打开的通道总数,或者返回 null。
async function numberOpenConnections (peerConnection) {
const stats = await peerConnection.getStats();
let peerConnectionStats = null;
stats.forEach((report) => {
if (report.type === "peer-connection") {
peerConnectionStats = report;
break;
}
});
result = (typeof peerConnectionStats.dataChannelsOpened === 'undefined' || typeof peerConnectionStats.dataChannelsClosed=== 'undefined') ? null : peerConnectionStats.dataChannelsOpened - peerConnectionStats.dataChannelsClosed;
return result
}
规范
| 规范 |
|---|
| WebRTC 统计 API 的标识符 # dom-rtcstatstype-peer-connection |
浏览器兼容性
加载中…