Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close stream #25

Open
renanyoy opened this issue Dec 26, 2015 · 4 comments
Open

close stream #25

renanyoy opened this issue Dec 26, 2015 · 4 comments

Comments

@renanyoy
Copy link

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

@renanyoy
Copy link
Author

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
Copy link
Author

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
Copy link

naffiq commented Mar 3, 2016

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

ajfranzoia added a commit to mediainbox/StreamMachine that referenced this issue Jun 27, 2017
The socket of icecast request was kept open even if the request itself was ended
via the end() method.

See related issues:
- TooTallNate/node-icy#25
- TooTallNate/node-icy#22
@renanyoy
Copy link
Author

renanyoy commented Sep 19, 2018

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants