获取网络数据时,获取到的数据为空
来源: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回答
-
参考我在慕课网上传的例子,仔细对比一下区别
012019-05-16 -
wangzhumo
提问者
2019-05-16
自己粗心哈.
setDataSeries([bitrateSeries]);
看到addPoint,还以为它自己就是字典那....
00
相似问题