I'm using node.js with the net module on my backend and flash on my frontend. I want to pass objects with json format. How would I know when all data has arrived in my backend so I can then do a JSON.parse?
My actionscript:
var obj = new Object();
obj.msg = "I want pizza";
xmlSocket.send(JSON.encode(obj));
My javascript:
socket.on("data", function(data){
var msg = JSON.parse(data.toString());
var i = 0;
while(i < clientData.length){
var client = clientData[i];
if(client.writable){
client.write(msg.msg, 'utf8');
}
i++;
}
});
At the moment I'm getting an error, which I'm not sure has to do with this since it displays the whole object:
undefined:1
{"msg":"I want pizza"}
^
SyntaxError: Unexpected token
To know when the data has ended, you have to add something in front that specifies the length or something at the end that shows that the data is over. So, either put the length in front of the JSON or put a newline after each JSON object.
Also: What's JSON.encode? I only know JSON.stringify.