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);
});
//...
};
Esiste un modo per leggere solo una parte definita dei dati? Ad esempio:Leggere solo i primi N byte dal socket in node.js
c.on('data', N, function(data) {
//Read first N bytes
});
Dove N è il numero di byte che mi aspetto. Quindi il callback ottiene solo N byte fuori M.
La soluzione è (grazie a MSCDEX):
c.on('readable', function() {
var chunk,
N = 4;
while (null !== (chunk = c.read(N))) {
console.log('got %d bytes of data', chunk.length);
}
});
Chi downvoted? Per favore, commenta! – Max