mafintosh / torrent-stream

The low level streaming torrent engine that peerflix uses
MIT License
1.95k stars 226 forks source link

Stop streaming when post variable is set/a link is visited #34

Open gekkeabt opened 10 years ago

gekkeabt commented 10 years ago

I want to stop streaming when the user clicks on a button which will set the post variable url as close. Right now I have this, but it says engine is not defined. Which is true. But I am having a hard time solving this.

var url = req.body.torrent;
var indexOfMovie;
if(url=="close"){
    res.sendfile('index.html');
    engine.remove();
    console.log("page closed");

}else{
    var engine = torrentStream(url,{
        path: 'tmp'
    });

    engine.on('ready', function() {
        engine.remove();            
        var server = http.createServer(function(req, res) {
                server.on("listening", function () { server.close(); });
                if(url!="close"){
                    var file = engine.files[indexOfMovie];
                    res.setHeader('Content-Length', file.length);
                    file.createReadStream().pipe(res);
                }
        });
        server.listen('2020');

    }); 
}

This should probably be done by stopping the server and removing the engine. I tried that with server.close() if server is listening but that isn't doing much.

mafintosh commented 9 years ago

(this is old but i'm gonna answer anyway) moving the engine declaration up to the beginning of the file should fix your issue.