TooTallNate / node-icy

Node.js module for parsing and/or injecting ICY metadata
MIT License
291 stars 48 forks source link

close stream #25

Open renanyoy opened 8 years ago

renanyoy commented 8 years ago

I'm using node-icy to scan streams, but all streams stay opened... how I can close/destroy a stream response ?

renanyoy commented 8 years ago

I'v fixed it, using

res.on('data', function(data) {
    if(res.res&&res.res.client)
       res.res.client.destroy();
});

but maybe it would be nice to add a function res.close() or res.destroy(); who does it directly

renanyoy commented 8 years ago

also, you can use a shorter way than overriding request, by using directly net sockets like here https://gist.github.com/renanyoy/ced3fbd6bc07219d0b7e

function rawget(url, fn) {
    var u = require('url').parse(url);
    require('dns').resolve(u.hostname, function(err, addresses) {
        var ip=u.hostname;
        if(addresses) 
            ip = addresses[0];
        var client = new require('net').Socket();
        client.connect(u.port, ip, function() {
            client.write('Get ' + u.path + ' HTTP/1.0\r\n');
            client.write('User-Agent: Mozilla/5.0\r\n');
            client.write('\r\n');
        });
        client.on('data', function(data) {
            fn(data);
            client.destroy();   // short message, needs streaming for long ones
        });
        client.on('close', function() {
            console.log('socket.close');
        });
    });
}
naffiq commented 8 years ago

Thank you for your effort! This method saved me tons of time!

renanyoy commented 6 years ago

fix 2018 to avoid some exceptions you need to destroy the response outside the data callback

function icyDestroy(res) {
    setTimeout(function() {
        if (res.destroy) 
            res.destroy();
        if (res.client && res.client.destroy) 
            res.client.destroy();        
        if (res.res && res.res.client && res.res.client.destroy) 
            res.res.client.destroy();
    },1);
}