node.js - 如何使用 node.js 流式传输 mjpeg
问题描述
我需要使用通过 HTTP 流式传输 mjpeg 文件的 restify 创建一个服务器脚本,问题是我首先不知道我应该为此遵循哪些步骤,例如我有几张图片,我应该将它们组合为 mjpeg首先文件然后通过 HTTP 响应发送这个 mjpeg 文件,编码为 multipart/mixed 还是什么?感谢您的回答。正如我从其他 .mjpeg 服务器所理解的那样,响应应该永远不会结束?
解决方案
我已经创建了这种服务器脚本,但它没有按我的意愿工作,我的意思是如果我通过单击键盘上的 F5 刷新页面,响应会使用下一个图像重新加载。可能是restify框架造成的,没有restify这个运行良好。
server.get("/app", (req, res, next) => { res.writeHead(200, { 'Cache-Control': 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0', Pragma: 'no-cache', Connection: 'close', 'Content-Type': 'multipart/x-mixed-replace; boundary=--myboundary' }); setTimeout(() => { if(bufferIndex == bufferArray.length) return bufferIndex = 0 res.write(`--myboundary\nContent-Type: image/jpg\nContent-length: ${bufferArray[bufferIndex].length}\n\n`); res.write(bufferArray[bufferIndex]); bufferIndex++ }, 1000) next() }); socketio.on('connection', (socket) => { console.log(socket.id) }) server.listen(3000, function () { console.log('%s listening at %s', server.name, server.url); });
推荐阅读
- google-sheets - 如何使用数组公式计算每行的余额
- sql - 基于连接到另一个表的字段聚合未正确求和
- authentication - 是否可以在将授权数据保存在本地的同时从外部源进行身份验证?
- django - 身份验证中使用的散列+盐渍电子邮件 - 好还是坏的做法?
- jupyter-notebook - 指向 Google Colab 中本地笔记本的链接
- c# - 使用 LINQ 返回子对象
- javascript - 使用java脚本在html页面中显示csv数据
- scala - 在 Scala 中,在使用 Future 之前如何使用它的值?
- javascript - 随机 if/else 行为 - Rock, Paper, Scissors 游戏
- influxdb - InfluxDB如何转义方括号“[”