I need to send a JSON string to a number of TCP clients from a node.js TCP server.
In order to read the messages from the socket/stream on the client side I need to do some sort of message framing. One way to do this is to prefix the message with the length of the message as an array - then convert that to the buffer size for the message on the client side.
How would I do something like this in node.js/javascript on the server and then read it out on the client side using a .NET client?
Given this client side code, how would I frame the message correctly on the server side using javascript/node?
TcpClient client = new TcpClient(server, port);
var netStream = client.GetStream();
// read the length of the message from the first 4 bytes
byte[] b = new byte[4];
netStream.Read(b, 0, b.Length);
int messageLength = BitConverter.ToInt32(b, 0);
// knowing the length, read the rest of the message
byte[] buffer = new byte[messageLength];
netStream.Read(buffer, b.Length, buffer.Length);
var message = System.Text.Encoding.UTF8.GetString(buffer);