I have nodejs listening on tcp port and getting content from Flash XMLSocket. If I try to push a lot of data in one message from the flash ( XMLSocket.send(long_message) ), I always get the stream.on("data", function(d) { event when I want this to happen when the whole message is transmitted Flash XMLSocket transfers data as a UTF8 encoded string with a zero byte. How can I control my message sequence?
UPDATE
I found a similar question here . But there is no clear answer. I know that the end of my message should be a null byte, but could you give me an example of how to store an incomplete message and avoid matching with the next / parallel message.
UPDATE2
After maerics answer, I did something like
var server = net.createServer(function(stream) { var dataBlock = ""; stream.on("data", function(d) { processChunk(d); }); function processChunk(data) { var chunks = data.split("\0"); while (chunks.length > 1) { if (dataBlock.length > 0) { dataBlock += chunks.shift(); processIncompingMessage(dataBlock); dataBlock = ""; } else { processIncompingMessage(chunks.shift()); } } dataBlock += chunks.shift(); } }
source share