nodeJSvideostream,当用户断开或移动到不同的页面时,连接不会closures

我正在使用express来build立nodeJS中的videostream服务器,并使用“request-progress”模块来获取进度状态。 这是(videostream)正常工作。
但问题是,即使我closures浏览器或移动到下一页后,服务器仍然stream数据。 我可以在控制台中看到。

这里是相同的代码:

app.route('/media/*').get(function (req, res) { var originalUrl = "http://myactualserver.com"; var resourceUrl = originalUrl.split('media'); var requestURL = BASE_URL + resourceUrl[1]; req.on('close', function () { console.log('Client closed the connection'); }); var options = {}; progress(request(requestURL), { throttle: 1000, delay: 0, lengthHeader: 'content-length', }) .on('progress', function (state) { console.log('progress', state); }) .on('error', function (err) { console.log('err'); }) .on('end', function () { console.log('end'); }) .pipe(res); }); 

我试了下面的post,最后添加了

 req.on("close", function(){}); 

使用nodejs和Express进行videostream

node.js http服务器,检测客户端何时断开

即使客户端断开连接,Node.js服务器也会保持stream式数据

我的问题是:
1.如何调用“请求进度”的“错误”或“结束”function?
2.我将如何closuresstream媒体?
3.为什么console.log('Client closed the connection'); 被称为两次?

回答你的问题:

  1. 您可以使用events.EventEmitter发出

参考这个例子

https://github.com/IndigoUnited/node-request-progress/blob/master/test/test.js

或者,您可以在closuresfunction中使用上次进度状态。

  1. 不知道这里有什么问题,进度会持续到套接字closures。

  2. 如果您从浏览器(默认播放器)发出请求,则会发出两个请求。