2

I have the following code:

// Setting up the TCP:
var server = net.createServer(function(socket) {
console.log('Android Client connected');

socket.setNoDelay(true);

socket.on('data', function(data) {
  console.log(data.toString());
  var json = JSON.parse(data.toString());

  if(json.id == 1) {
    socket.world = findAnEmptyWorld();
    socket.player = socket.world.addPlayer(json.data);
    socket.player.socket = socket;

    tcpClients.push(socket);

    socket.write(JSON.stringify({id: 3, data: socket.player.toJSON()}));
    socket.pipe(socket);
  }
});
});

server.listen(4444, function() {
console.log("TCP server listening on port 4444");
});

When I write the message it is stored into a buffer but flushed only after I close the server... What should I do to flush the buffer immediately after write? (as you can see I tried 'setNoDelay' to true)

  • I played with a similar code and observed that using socket.write (somedata) and socket.pipe(socket) will trigger the 'data' event too, so we get somedata back to us. Just curious, did you observe the same thing? – Marc K. Apr 23 '17 at 18:14

1 Answers1

0

I should just put a new line after the end of string... what the f#?^&k.... like that

socket.write(JSON.stringify({id: 3, data: socket.player.toJSON()}) + "\n");