我正在实现一个 Node 服务器,它定期从网络摄像头抓取图像并通过 Node 模块Delivery.js将它们发送到客户端。 .
但是,查看我的浏览器(在 Chrome 开发工具中)使用的资源,似乎发送的每个图像都由客户端(或者可能由服务器?)无限期存储。
我使用的代码类似于 Delivery.js 自述文件中的“将文件推送到客户端”示例:
服务器代码
//set the camera to take a snapshot and send it for the required framerate
setInterval(function(){
//take a snapshot of the current view
cam.snapshot('./current_view.jpg' ,function( jpeg ) {
//send this snapshot to client
delivery.send({
name: 'current_view.jpg',
path : './current_view.jpg'
});
})
delivery.on('send.success',function(file){
//console.log('File successfully sent to client');
});
}, cameraUpdateDelay);
客户端代码
var delivery = new Delivery(socket);
delivery.on('receive.start',function(fileUID){
//console.log('receiving a file!');
});
delivery.on('receive.success',function(file){
if (file.isImage()) {
//change the src of the img tag to the new file
$('img').attr('src', file.dataURL());
console.log(file);
};
});
收到下一个文件后是否无法删除每个文件?
最佳答案
我刚刚快速浏览了客户端源代码,每个接收到的图像都被添加到一个对象中,图像的 uuid 是关键。如果您想限制内存泄漏,那么在接收到图像后从对象中删除图像可能是有意义的。上线https://github.com/liamks/Delivery.js/blob/master/lib/client/delivery.js第140行它被添加到对象中:
_this.receiving[file.uid] = filePackage;
在上面,如果你修改'receive.success',看起来像:
pubSub.subscribe('receive.success',function(filePackage){
_this.socket.emit('send.success',filePackage.uid);
delete _this.receiving[filePackage.uid];
});
这应该可以解决内存泄漏问题。但是,您可能需要进行一些测试以确认它不会破坏其他任何内容......
关于javascript - 客户端永久存储数据流中的所有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855120/