Using CreateReadStream in Webtorrent

i am new to webtorrent and want to stream a movie being downloaded using webtorrent, from my Linux server to the client browser, using nodejs. in the Doc of webtorrent i found stream = CreateReadStream() but I don’t know how to get that to the client computer. This is my code so far, which only created a video player window but no video shows up.

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,         'Content-Type': 'video/mp4',       }       res.writeHead(200, head)        torrent.files[4].createReadStream().pipe(res)       console.log("yes");     }   });   torrent.on('done', function () {     console.log('torrent download finished')   }) }) 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.