var server = net.createServer(function(c) { //... c.on('data', function(data) { //The data is all data, but what if I need only first N and do not need other data, yet. c.write(data); }); //... };
Is there a way to read only a specific piece of data? For instance:
c.on('data', N, function(data) {
Where N is the number of bytes I expect. Thus, the callback receives only N of M bytes.
Solution (thanks to mscdex):
c.on('readable', function() { var chunk, N = 4; while (null !== (chunk = c.read(N))) { console.log('got %d bytes of data', chunk.length); } });
source share