WebRTC在程序内进行监控网络等


相关链接

  • https://developer.mozilla.org/en-US/docs/Web/API/RTCStats/type
  • https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidateStats
  • https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidatePairStats

示例代码

<div id="statsBox"></div>

// 定时采集
let pc = new RTCPeerConnection();
setInterval(() => {
    
    
            pc.getStats(null).then((stats: any[]) => {
    
    
                let statsOutput = "";
                stats.forEach((report) => {
    
    
                    statsOutput +=
                        `<h2>Report: ${
      
      report.type}</h2>\n<strong>ID:</strong> ${
      
      report.id}<br>\n` +
                        `<strong>Timestamp:</strong> ${
      
      report.timestamp}<br>\n`;

                    // Now the statistics for this report; we intentionally drop the ones we
                    // sorted to the top above

                    Object.keys(report).forEach((statName) => {
    
    
                        if (
                            statName !== "id" &&
                            statName !== "timestamp" &&
                            statName !== "type"
                        ) {
    
    
                            statsOutput += `<strong>${
      
      statName}:</strong> ${
      
      report[statName]}<br>\n`;
                        }
                    });
                });
                let dom = document.querySelector("#statsBox") as HTMLElement;
                dom.innerHTML = statsOutput;
            });
        }, 5000);

示例结果

以下列出了项目关心的type

certificate 证书统计

RTCCertificateStats

ID: CF04:7B:B8:28:84:70:14:A3:4C:C6:50:05:1E:58:33:E2:88:88:63:EB
Timestamp: 1678257290072
// 证书的指纹,用于验证证书的完整性。
fingerprint: 04:7B:B8:28:84:70:14:A3:4C:C6:50:05:1E:58:33:E2:88:88:63:EB
// 指纹算法,如sha-1、sha-256等。
fingerprintAlgorithm: sha-256
// 证书的Base64编码形式。
base64Certificate: MIIBFjCBvKADAgECAggTNPE94mpV

candidate-pair ICE候选者对统计【重点】

RTCIceCandidatePairStats

ID: CPxzy3KCcQ_A2Yy6ZJR
Timestamp: 1678257345075
transportId: T01  // 与该候选者对相关的传输ID。
localCandidateId: Ixzy3KCcQ // 本地候选者ID。
remoteCandidateId: IA2Yy6ZJR // 远端候选者ID。
state: succeeded // 候选者对的连接状态,如frozen、waiting等。
priority: 9079301928688570000 // 优先级,用于选择最佳候选者对。
nominated: true // 表示该候选者对是否已被选择。
writable: true // 是否可以接收数据
packetsSent: 137 // 已发送的数据包数量。
packetsReceived: 136 // 已接收的数据包数量。
bytesSent: 8905  // 已发送的数据字节数。
bytesReceived: 8840 // 已接收的数据字节数。
totalRoundTripTime: 20.002 // 总的往返时延。【重点】
currentRoundTripTime: 0.002 // 当前的往返时延。【重点】
requestsReceived: 1130 // 已接收的请求的数量。
requestsSent: 1156  // 已发送的请求的数量。
responsesReceived: 1156 // 已接收的响应的数量。
responsesSent: 1130 // 已发送的响应的数量。
consentRequestsSent: 1155 //已发送的同意请求的数量。
packetsDiscardedOnSend: 0 // 发送时丢弃的数据包 【重点】
bytesDiscardedOnSend: 0  // 发送时丢弃的字节 【重点】
lastPacketReceivedTimestamp: 1678257317575 // 接收到该候选对的最后一个数据包的时间
lastPacketSentTimestamp: 1678257317575    // 发送到该候选对的远程对等点的时间

data-channel 数据通道统计

RTCDataChannelStats

ID: D284
Timestamp: 1678257510071
label: 19eefb4bb8d18ffe2dc143963bb9096a3ca84b4e // 数据通道的标签。
protocol:  // 数据通道使用的协议。
dataChannelIdentifier: 1 // 数据通道的ID。
state: open //数据通道的状态,如open、closing、closed等。
messagesSent: 0 // 已发送的消息数量。
bytesSent: 0 // 已发送的数据字节数。
messagesReceived: 0 // 已接收的消息数量。
bytesReceived: 0 // 已接收的数据字节数。

local-candidate 本地ICE候选者统计

RTCIceCandidateStats

ID: Ij2n3mC7t
Timestamp: 1678257825072
transportId: T01
isRemote: false
// 在开启音频/视频或者禁用 mdns的时候会出现
networkType: wifi  //  wifi/unknown/ethernet
// 在开启音频/视频或者禁用 mdns的时候会出现
ip: 172.23.80.1 
// 在开启音频/视频或者禁用 mdns的时候会出现
address: 172.23.80.1
port: 9
protocol: tcp
candidateType: host
priority: 1509949951
foundation: 1740889942
usernameFragment: N3DS
tcpType: active

remote-candidate 远程ICE候选者统计

RTCIceCandidateStats

ID: IA2Yy6ZJR
Timestamp: 1678257805887
transportId: T01 //用于关联传输统计信息的ID。
isRemote: true // 表示该候选者是否是远端候选者。
ip: 172.23.80.1          // 候选者的IP地址。
address: 172.23.80.1    // IPv4 地址、IPv6 地址或完全限定的域名
port: 53587   // 候选者的端口号。
protocol: udp // 候选者的传输协议,如UDP、TCP等。
candidateType: host // 候选人类型 host:主机实际上是远程对等方的真实地址 srflx/prflx/relay
priority: 2113939711 //候选者的优先级。
foundation: 2638551502
usernameFragment: mpnD

peer-connection 连接统计

RTCPeerConnectionStats

ID: P
Timestamp: 1678257955071 
dataChannelsOpened: 1 // 已打开的数据通道数量。
dataChannelsClosed: 0 // 已关闭的数据通道数量。

transport 传输统计【重点】

RTCTransportStats

ID: T01
Timestamp: 1678257900070 // 统计信息的时间戳。
bytesSent: 11850  // 已发送的字节数
packetsSent: 164  // 已发送的数据包数量。
bytesReceived: 11321 // 已接收的字节数。
packetsReceived: 164 // 已接收的数据包数量。
dtlsState: connected
// 选择的候选对ID。
selectedCandidatePairId: CPxzy3KCcQ_A2Yy6ZJR
// 本地证书ID。
localCertificateId:  CFD1:F8:59:06:1E:83:8E:FC:CE:A6:DE:BE:BA:F0:2B:C4:5C:00:87:6A:CB:DC:88:F5:6D:17:14:E9:60:41:75:E7
// 远端证书ID。
remoteCertificateId: CF04:7B:B8:28:84:70:14:A3:4C:C6:50:05:1E:58:33:E2:88:88:63:EB:AE:E6:5F:8C:3F:0C:DF:81:31:42:05:21
tlsVersion: FEFD  // TLS协议的版本号。
dtlsCipher: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 // DTLS密钥交换过程中使用的密码套件。
dtlsRole: server // // DTLS密钥交换过程中的角色 client/server,主动发起者offer 是 server
srtpCipher: AES_CM_128_HMAC_SHA1_80 //SRTP加密所使用的密码套件。
selectedCandidatePairChanges: 2  // 由于网络状况变化而导致选择候选者对的更改次数。【重点】
iceRole: controlling
iceLocalUsernameFragment: N3DS
iceState: connected

猜你喜欢

转载自blog.csdn.net/abu935009066/article/details/129416344