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

Concat received data parts in client #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alxppp
Copy link

@alxppp alxppp commented Apr 26, 2017

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

@smblott-github
Copy link
Owner

smblott-github commented Apr 26, 2017

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.

@Thr1ve
Copy link

Thr1ve commented Aug 11, 2017

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

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

Successfully merging this pull request may close these issues.

3 participants