大网数据广告活动
Web 标准与前端开发

RTCPeerConnectionStats

· 52CE 我爱测

The **`RTCPeerConnectionStats`** dictionary of the WebRTC API provides information about the high-level peer connection (RTCPeerConnection).

WebRTC

The RTCPeerConnectionStats dictionary of the WebRTC API provides information about the high-level peer connection (RTCPeerConnection).

In particular, it provides the number of unique data channels that have been opened, and the number of opened channels that have been closed. This allows the current number of open channels to be calculated.

These statistics can be obtained by iterating the RTCStatsReport returned by RTCPeerConnection.getStats() until you find a report with the type of peer-connection.

Instance properties

  • RTCPeerConnectionStats.dataChannelsOpened
    • : A positive integer value indicating the number of unique RTCDataChannel objects that have entered the open state during their lifetime.
  • RTCPeerConnectionStats.dataChannelsClosed
    • : A positive integer value indicating the number of unique RTCDataChannel objects that have left the open state during their lifetime (channels that transition to closing or closed without ever being open are not counted in this number). A channel will leave the open state if either end of the connection or the underlying transport is closed.

Common instance properties

The following properties are common to all WebRTC statistics objects.

  • RTCPeerConnectionStats.id
    • : A string that uniquely identifies the object that is being monitored to produce this set of statistics.
  • RTCPeerConnectionStats.timestamp
    • : A DOMHighResTimeStamp object indicating the time at which the sample was taken for this statistics object.
  • RTCPeerConnectionStats.type
    • : A string with the value "peer-connection", indicating the type of statistics that the object contains.

Examples

This example shows a function to return the total number of open connections, or null if no statistics are provided. This might be called in a loop, similar to the approach used in RTCPeerConnection.getStats() example

The function waits for the result of a call to RTCPeerConnection.getStats() and then iterates the returned RTCStatsReport to get just the stats of type "peer-connection". It then returns the total number of open channels, or null, using the data in the report.

async function numberOpenConnections(peerConnection) {
  const stats = await peerConnection.getStats();
  let peerConnectionStats = null;

  for (const report of stats.values()) {
    if (report.type === "peer-connection") {
      peerConnectionStats = report;
      break;
    }
  }

  if (
    peerConnectionStats === null ||
    typeof peerConnectionStats.dataChannelsOpened === "undefined" ||
    typeof peerConnectionStats.dataChannelsClosed === "undefined"
  ) {
    return null;
  }

  return (
    peerConnectionStats.dataChannelsOpened -
    peerConnectionStats.dataChannelsClosed
  );
}

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。