I have a very basic tcp server that can send and receive json data.
See my code:
// Handle incoming messages from clients.
socket.on('data', function (data) {
var obj = JSON.parse(data);
if(obj.type == "register") {
_clientObj[obj.msg] = {
client: 'Client',
socketID: socket.name
}
console.log("Register...");
} else if(obj.type == "message") {
console.log(socket.name + " --> message: " + obj.msg);
}
});
The problem seems to be that sometimes data is not complete and it trows an error:
SyntaxError: Unexpected end of input
How can i wait before data is finished before i parse out the json?