Reputation: 993
I'm trying to utilise TCP sockets in Node.js to communicate with a Lua program. There are two issues I'm trying to get my head around. Firstly buffers.
Buffers As I understand it, when data is provided to your code from the socket, it will be whatever data has been received so far (a stream rather than packets). So when you read the data received, it may be broken from what was sent.
i.e. data received { schools : ["Long
vs data sent { schools : [{"Longwood", "Hillbrow"}] }
The way to get around this is to put your data into a 'buffer' and split it by whatever method you use to show the end of that piece of information. Commonly appears to be new line.
My questions here:
Finally I seem to be misunderstanding the data side. Does all data sent and received need to be converted to binary and back? I wish to send JSON data back and forth only. I think there in lies my confusion. For example:
var myQuestion = "Is this acceptable and will I encounter any issues?
socket.write(myQuestion);
Many thanks for your time.
Upvotes: 4
Views: 6568
Reputation: 161447
setEncoding
.First off, Buffer
is binary. A buffer is simply an array of bytes, and nothing more. Buffers can be created from strings, and node will do this automatically in some cases. In your case, I would recommend that you call socket.setEncoding('utf8')
. That will automatically convert incoming data into a string to simplify your parsing.
As far as processing and splitting the data, it is up to you. TCP only provides a stream of bytes arriving in the same order they were send. As you said, you can gather up the bytes and when a newline is received, you take everything before that and parse it as JSON. This should work fine. You can use any character that wouldn't pop up inside JSON. As long as the programs doing the JSON serialization do not add newlines, then you are set. As 'data' is emitted, you can check the string for newline, and if not, then add it to any previously received data, and if you find it, then split it up, add the existing data.
Upvotes: 5
Reputation: 163272
Buffers are used for information sent and received. Data received can be fragmented, as you have noticed. It is always up to your code to make this data back into its correct pieces.
It is entirely possible to have two chunks of data received in a buffer. Usually, you will have a delimiter of some kind (such as a new line, like you mentioned). What I do is this:
Upvotes: 1