chromix-too
chromix-too copied to clipboard
Concat received data parts in client
Fixes #6
The nodejs socket server sends data over in 65536B chunks [1], this commit concats them. Note that the != 65536
is quite hacky and shoudn't be merged. It should instead rely on the end
event of the client [1], which I couldn't get to work unfortunately.
[1] https://nodejs.org/api/net.html [2] http://stackoverflow.com/questions/36397950/how-to-send-file-over-tcp-in-one-time-in-nodejs
Thanks, @alxppp.
You're right. We do need to detect the end of the data correctly.
An alternative ~~better~~ hack would be to try JSON-parsing the data.
I fixed this by adjusting what we add to the responseHandlers
map here like this:
responseHandlers[myClientId] = function(...args) {
sock.write.call(sock, ...args);
sock.end.call(sock);
};
And then adding a listener for the end
event and refactoring the logic a bit here like this:
let buffer = Buffer.from('');
// ...
client.on('end', function() {
debug('end event received');
let response;
try {
response = JSON.parse(buffer.toString('utf8'));
buffer = '';
} catch (e) {
debug('ERROR PARSING RESPONSE', e);
return;
}
if (response.error) {
debug(`error: ${response.error}`);
process.exit(1);
}
for (let callback of callbacks) {
callback(...Array.from(response.response || []));
}
client.destroy();
});
client.on('data', function(data) {
debug('data received, adding to buffer');
buffer = Buffer.concat([buffer, data]);
});
I'm not using coffee-script obviously, but hopefully this can still help! 😄