So i wrote server tcp application with nodejs, here is the code:
Server Side
var host = "127.0.0.1";
var port = 15010;
// Instruct server to start Listening for incoming connection
function listen() {
// Start Server
console.log('Server Started [' + host + ':' + port + ']');
var server = net.createServer(function(socket) {
// Send Connection Success
socket.write('Success');
socket.pipe(socket);
// Server Received a new Connection from Client
console.log('Client was Connected [' +
socket.remoteAddress + ':' + socket.remotePort + ']');
// When Server Received data from Client.
socket.on('data', function(resp) {
console.log('We received following data: ' + resp);
// probably going to tell server whether my Game is about to exit
// so the server will close the current socket.
// and also won't caught ECONNRESET exception lol.
});
// When Client Connection is Ended.
socket.on('end', function() {
console.log('Connected Ended ');
// Destroy closed socket
socket.destroy();
});
// When Client Connection is Closed.
socket.on('close', function() {
console.log('Client was Closed ');
// Destroy closed socket
socket.destroy();
});
// When Server caught an exception
socket.on('error', function(ex) {
// Server will caught an exception if client was disconnected from client side
// It's appear the server was not expected when client is disconnected.
// Currently, i will salvage it as much as i can until i find better solution (or this is the best solution(?) lol) but it's doesn't matter for now..
if (ex.code == 'ECONNRESET') {
console.log('Ending current session of Client');
}
});
}).listen(port, host);
it's work well when handling incoming string data.
However i was wondering how to parse a binary data that received by the server from client.
I am using following code in my game to send data to server:
Client Side
// Use memory stream
using (MemoryStream ms = new MemoryStream())
{
// Use binary writer also
BinaryWriter writer = new BinaryWriter(ms);
// Write int16 (2 bytes)
writer.Write((short)id);
// Write int32 (4 bytes)
writer.Write((int)status);
// Write int64 (8 bytes)
writer.Write((long)score);
// Write float (4 bytes)
writer.Write((float)freq);
// I can even write string on it easily (assuming username string is 2 chars)
writer.Write(Encoding.UTF8.GetBytes(username));
// The stream position should be
// 2 + 4 + 8 + 4 + 2 = 20
Console.WriteLine(ms.Position.ToString());
// Send data to server
socket.Send(ms.GetBuffer());
}
In C#, I could handle it easily with following code (the code is also similar like send data too):
// Receiving data from the Server
byte[] resp;
socket.Receive(out resp);
// Could be handled with Memory Stream too
using (MemoryStream ms = new MemoryStream(resp))
{
// This time i will use BinaryReader
BinaryReader reader = new BinaryReader(ms);
// Read int16 (2 bytes)
short id = reader.ReadInt16();
// Read int32 (4 bytes)
int status = reader.ReadInt32();
// Read int64 (8 bytes)
long score = reader.ReadInt64();
// Read float (4 bytes)
float freq = reader.ReadSingle();
// Read String (assuming data is 2 bytes)
string username = Encoding.UTF8.GetString(reader.ReadBytes(2));
// Same like before
// The stream position should be
// 2 + 4 + 8 + 4 + 2 = 20
Console.WriteLine(ms.Position.ToString());
}
but how i can parse it in NodeJS?
can someone tell me which classes in NodeJS that i should use to achieve this? (providing an example like I did in C# is much more appreciated)
any ideas?
sorry for my bad English, thanks in advance
EDIT
So I figured a bit, it seem received data is a Buffer
object as default (thanks for @dandavis)
here my attempt and its working:
// When Server Received data from Client.
socket.on('data', function(resp) {
// It's kinda hard (not effective(?))
// since i need to specify offset each time im trying to read something
// probably i will write simple class, or does anyone know class
// that could handle like BinaryReader in C#?
// Read int16 (2 bytes)
var id = resp.readInt16LE(0);
// Read int32 (4 bytes)
var status = resp.readInt32LE(2);
// I don't know how to read int64
var score = // there is no readInt64LE ??
// Read float (4 bytes)
var freq = resp.readFloat(14);
// Read string, assuming encoded with utf8 with 2 characters
var username = resp.toString('utf8', 18, 2);
});
But there are few problem:
- I don't know how to read (and write) int64
- in API doc, it's doesn't describe what the difference between LE and BE (i can only speculate it was Little Endian and Big Endian, but i don't know them)
Could you guys tell me how to read int64 and give short explanation about LE and BE?
thanks!