1.前言
项目需求:摄像头通过rtsp推流,前端利用webrtc流实时播放,但是会出现网络不好,或者断网,断点的情况,所以需要实时检测设备的在线情况。
2. 思路
之前的方案是通过播放webrtc流去判断是否在线,但是由于服务器解码webrtc对cup的性能消耗比较大,所以最终还是采用node检测去ip的方案!每隔个5秒去检测一次,这样算是性能上比较友好了
node代码:
let ping = require('ping'),
axios = require('axios');
router.post('/pingIp', function (req, res, next) {
let ips = req.body.ips,
promiseArr = [];
ips.forEach(function (ip) {
let promise = ping.promise.probe(ip, {
timeout: 2,
extra: ['-i', '2'],
})
promiseArr.push(promise);
});
//检测所有的ip
axios.all(promiseArr).then((response) => {
res.send({
"code": 200,
"message": "成功",
"result": "success",
"content": response
});
});
});
module.exports = router;
前端主要代码:
async created() {
//获取当前页的设备列表
let ips = await this.getMonitorVos(this.lampCurrentPage, this.pampPageSize);
await this.getIpStatus(ips);
await this.getReplayList(this.fileCurrentPage, this.filePageSize);
this.ipsTime = setInterval(async () => {
let ips = await this.getMonitorVos(
this.lampCurrentPage,
this.pampPageSize,
true
);
ips.length != 0 && (await this.getIpStatus(ips));
}, 5000);
},
效果如下: