Reputation: 331
I'm working on a JS client and C# server connection.
The connection is going well. The problem occurs when I want to send a buffer from the JS client to the C# server.
For the buffer to be processed by my server, the buffer must have a particular structure: A header encoded on 18 bytes, followed by the message.
Buffer contains:
In a previous version I used a C # client to send this buffer. I proceed like this:
//Header parameters
ulong ID = ulong.MaxValue;
ushort type = 0;
//Message
string message = "This is the message";
byte[] encodedMessage = Encoding.ASCII.GetBytes(message);
//Encoding the header and add header to buffer
byte[] buffer = new byte[encodedMessage.Length + 18];
Array.Copy(BitConverter.GetBytes(encodedMessage.LongLength + 18), 0, buffer, 0, 8);
Array.Copy(BitConverter.GetBytes(ID), 0, buffer, 8, 8);
Array.Copy(BitConverter.GetBytes((uint)type), 0, buffer, 16, 2);
//Adding the message to the buffer
Array.Copy(encodedMessage, 0, buffer, 18, encodedMessage.Length);
Header informations are converted with the C# BitConverter.GetBytes() function.
I would like to do the same in NodeJs
I did this:
var msg = "This is the message"
var size = 18 + msg.length
var buffer = new ArrayBuffer(size)
var buf = Buffer.from(buffer);
// Writting the header
buf.write(str(size-18),0,8) // Write the message size
buf.write("123456",8,8) // Write the id
buf.write("0",16,2) // Write the message type
//Adding the message to the buffer
buf.write(msg,18,msg.length)
But in this way the header is not converted to a bit like the C # function above.
Is there a method to do this in nodejs ?
Upvotes: 1
Views: 1554
Reputation: 395
You can allocate the amount of bytes fro a buffer using Buffer.alloc
method. The method takes an integer that will indicate the length of the buffer. If you create a buffer with the new keyword then you are using Buffer.allocUnsafe
method under the hood so, therefore it recommended to create buffers without new keyword. With that said here is the solution I came up with
const msg = 'This is a message'
const buffer = Buffer.alloc(18)
let bytesWritten = 0;
// Write the first 8 bytes
bytesWritten += buffer.write(msg, 0, 8)
console.log('I wrote ',bytesWritten,' bytes');
// Write the next 8 bytes
bytesWritten += buffer.write(msg, (buffer.length - bytesWritten), 8);
console.log('I have now written ',bytesWritten,' bytes');
// Write the remaining 2 bytes
buffer.write(msg, (buffer.length - bytesWritten), 2);
Upvotes: 0
Reputation: 24555
You can use Buffer.alloc
to initialize the header buffer with a length of 18 bytes. Then write the data with something like this and finally concat the header buffer with the content buffer:
const content = "This is the message";
const headerBuffer = Buffer.alloc(18);
headerBuffer.write(Buffer.from(content, 'ascii').byteLength.toString(), 0, 8);
headerBuffer.write("1234", 8, 8);
headerBuffer.write("0", 16, 2);
const contentBuffer = Buffer.from(content, 'ascii');
const result = Buffer.concat([headerBuffer, contentBuffer]);
Writing this buffer to a file and displaying it with hexdump
results in:
$ hexdump -C tmp
00000000 31 39 00 00 00 00 00 00 31 32 33 34 00 00 00 00 |19......1234....|
00000010 30 00 54 68 69 73 20 69 73 20 74 68 65 20 6d 65 |0.This is the me|
00000020 73 73 61 67 65 |ssage|
00000025
Upvotes: 0