我用 nodejs 创建了一个小应用程序,它将 multipart/x-mixed-replace 数据流式传输到浏览器。
这个数据是用图像数据创建的,但是图像数据可能会随着时间而改变,所以在浏览器中它看起来像一个视频。图像数据是从网络摄像头创建的,因此在浏览器中它看起来像实时流媒体。
但是性能不是很好。
我尝试了其他一些方法: - 首先:使用 socket.io 将图像推送到浏览器,这里我使用图像中的 base64 数据(推送此数据)并在浏览器中重新创建图像(jpeg):效果很好,但只有一两个客户。_ 第二:使用从浏览器到 nodejs 服务器的轮询.. 这不喜欢我。
所以这是代码:(我的nodejs服务器的部分代码)我使用express来制作http服务器:
app.get('/videoStream',function(req,response){
response.writeHead(200,{
'Content-Type': 'multipart/x-mixed-replace;boundary="' + boundary + '"',
'Connection': 'keep-alive',
'Expires': 'Fri, 01 Jan 1990 00:00:00 GMT',
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma': 'no-cache'
});
response.write('--'+boundary+'\n');
events.addListener('imagen_recibida',function(){
fs.readFile(__dirname + '/image.jpeg',function(err,data){
if(err) return send404(response);
response.write('Content-Type: image/jpeg\n Content-Length: '+data.length+'\n\n');
response.write(data);
response.write('\n--'+boundary+'\n');
});
});
当事件“imagen_recibida”上升时,它从磁盘读取图像并将数据写入浏览器。
所以两个问题:
有什么方法可以提高性能吗?(将图像写入磁盘然后读取以发送到浏览器看起来不是一个好技巧)
有没有办法将其编码为另一种格式以提高性能?
非常感谢。
PD:图像被写入磁盘,然后读取以发送到浏览器,因为我通过 RPC 调用从另一个函数中的另一个进程接收图像数据。