【问题标题】:How to end a request.js ServerResponse stream that never ends如何结束一个永远不会结束的 request.js ServerResponse 流
【发布时间】:2020-11-07 04:57:19
【问题描述】:

我有这段代码可以根据命令抓取视频源,但一旦启动它就永远不会停止

async function getBandonBeachesOne(req, res, next) {
  let timer = 0;
  let videoURL =
    "http://dizzyisadog.viewnetcam.com:50000/nphMotionJpeg?Resolution=640x480&Quality=Clarity";
  let videoStream = request
    .get(videoURL)
    .on("data", function (data) {
      timer++;
      if (timer > 30) {
        timer = 0;
        console.log("received " + data.length + " bytes of compressed data");
      }
    })
    .on("end", () => {
      console.log("end");
    })
    .pipe(res);

  req.on("close", () => {
    console.log("Client left");
    videoStream.end("end");
    videoStream.destroy();//data keeps comming from the request.get(videoURL)
  });
}

这是一个请求处理程序,它获取视频源并将其通过管道传输到客户端。客户离开后,我会收到 "Client left" 消息,但我无法尝试,我无法阻止 on('data') 事件表单永远触发

【问题讨论】:

标签: node.js stream pipe


【解决方案1】:

感谢answer here 我发现解决方案是 videoStream.abort()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2013-04-09
    • 2010-12-27
    • 1970-01-01
    • 2011-01-20
    • 2017-11-05
    • 2019-05-21
    相关资源
    最近更新 更多