在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 (将#修改为@)