获取网络数据时,获取到的数据为空

来源:13-6 【来点实战】WebRTC统计信息-2

wangzhumo

2019-04-26

代码如下:


function getSenderState(states) {
    states.forEach((state) => {
        if (state.type === 'outbound-rtp' && !state.isRemote) {
            let curTimestamp = state.timestamp;
            let bytes = state.bytesSent;
            let packets = state.packetsSent;
            //不为空且是同一个report
            if (lastReports && lastReports.has(state.id)) {
                //计算平均值
                let lastReprt = lastReports.get(state.id);
                let bitrate = 8 * (bytes - lastReprt.bytesSent) / (curTimestamp - lastReprt.timestamp);

                bitrateSeries.addPoint(curTimestamp, bitrate);
                bitrateGraph.setDataSeries(bitrateSeries);
                bitrateGraph.updateEndDate();

                packetSeries.addPoint(curTimestamp, packets - lastReprt.packetsSent);
                packetGraph.setDataSeries(packetSeries);
                packetGraph.updateEndDate();
            }
        }
    });
    lastReports = states;
}

//定时器 1s 执行一次
window.setInterval(() => {
    if (!peerConnect) {
        return;
    }
    console.log("setInterval run");
    let sender = peerConnect.getSenders()[0];
    if (!sender) {
        console.log("sender is null.");
        return;
    }
    //获取状态
    sender.getStats()
        .then(getSenderState)
        .catch(handleError);
}, 1000);

代码可以正常运行,也可以达到两端通信
获取:
let sender = peerConnect.getSenders()[0];
serder
不为空,进行到下一步:
sender.getStats().then(getSenderState )
getSenderState方法中
*states 的长度为 0 *

写回答

2回答

李超

2019-04-26

参考我在慕课网上传的例子,仔细对比一下区别

0
1
wangzhumo
非常感谢!
2019-05-16
共1条回复

wangzhumo

提问者

2019-05-16

自己粗心哈.

setDataSeries([bitrateSeries]);

看到addPoint,还以为它自己就是字典那....

0
0

5G时代必备音视频WebRTC实时互动直播技术入门与实战

低门槛掌握WebRTC技术,高效率快速构建一套音视频实时互动系统

2557 学习 · 731 问题

查看课程