How do I frame messages for sending over TCP with node.js?
to unframe incoming data in nodejs you can try to use node-bufferlist or node-buffers or create your own FSM manually and feed it with incoming chunks of data
server side is simplier:
function sendPacket(stream, buffer)
{
var prefix = new Buffer(4);
var length = buffer.length;
var offset = 0;
// serialize 32bit little endian unsigned int
prefix[offset++] = length & 0xff;
prefix[offset++] = (length >> 8) & 0xff );
prefix[offset++] = (length >> 16) & 0xff );
prefix[offset++] = (length >> 24) & 0xff );
stream.write(prefix);
stream.write(buffer);
}
or you can use node v0.5+ buffer.writeUInt32