微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Webtorrent中使用CreateReadStream

如何解决在Webtorrent中使用CreateReadStream

我是webtorrent的新手,并且想使用nodetorrent将使用webtorrent下载的电影从Linux服务器流式传输到客户端浏览器。在webtorrent的文档中,我发现stream = CreateReadStream(),但我不知道如何将其发送到客户端计算机。到目前为止,这是我的代码,该代码仅创建了一个视频播放器窗口,而没有视频显示

client.add(torrentId,{ path: __dirname + '/torrent' },function (torrent) {
  for (var i = 0; i < torrent.files.length; i++) {
    console.log(torrent.files[i].name);
  }
  app.get('/video',function(req,res) {
    const fileSize = torrent.files[4].length
    const range = req.headers.range
    if (range) {
      const parts = range.replace(/bytes=/,"").split("-")
      const start = parseInt(parts[0],10)
      console.log("yesb");
      const end = parts[1]
        ? parseInt(parts[1],10)
        : fileSize-1
      const chunksize = (end-start)+1
      const file =  torrent.files[4].createReadStream([{start: start,end: end}])
      const head = {
        'Content-Range': `bytes ${start}-${end}/${fileSize}`,'Accept-Ranges': 'bytes','Content-Length': chunksize,'Content-Type': 'video/mp4',}
      res.writeHead(206,head);
      file.pipe(res);
    } else {
      const head = {
        'Content-Length': fileSize,}
      res.writeHead(200,head)
       torrent.files[4].createReadStream().pipe(res)
      console.log("yes");
    }
  });
  torrent.on('done',function () {
    console.log('torrent download finished')
  })
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。