chromix-too icon indicating copy to clipboard operation
chromix-too copied to clipboard

Concat received data parts in client

Open alxppp opened this issue 7 years ago • 2 comments

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

alxppp avatar Apr 26 '17 14:04 alxppp

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.

smblott-github avatar Apr 26 '17 14:04 smblott-github

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! 😄

Thr1ve avatar Aug 11 '17 18:08 Thr1ve