我正在使用express在nodeJS中构建一个视频流服务器,并使用“request-progress”模块来获取进度状态.
这是(视频流)工作正常.
但问题是,即使我关闭浏览器或移动到下一页,服务器仍然流数据.我可以在控制台中看到它.
这是相同的代码:
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);
});
我尝试了以下帖子,最后添加了
req.on("close", function(){});
Video Streaming with nodejs and Express
node.js http server, detect when clients disconnect
Node.js server keeps streaming data even after client disconnected
我的问题是:
1.如何调用“请求进度”的“错误”或“结束”功能?
2.我将如何关闭流媒体?
3.为什么选择console.log(‘客户端关闭了连接’);
被叫两次?
最佳答案 您的问题的答案:
>您可以使用events.EventEmitter中的emit
请参阅此示例
https://github.com/IndigoUnited/node-request-progress/blob/master/test/test.js
或者,您可以在关闭函数中使用上一个进度状态.
>不确定这里的问题是什么,进度将持续到套接字关闭.
>如果您是从浏览器(默认播放器)发出请求,则会发出两个请求.