我需要使用restify创建一个服务器脚本,通过HTTP流式传输mjpeg文件,问题是我不知道首先应该遵循什么步骤,比如我有几张图片,我应该先把它们合并成mjpeg文件,然后通过HTTP响应发送这个mjpeg文件,像multipartmixed这样的编码还是什么?谢谢你的回答。我从其他.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); });