encodePacket(packet)
Creates a Buffer that holds the encoded version of the Packet you supplied to the function.
You will use it when you want to send a request/response to the server/client (depends on if you are creating server or a client).
packet
A Packet object.Buffer
: A instance of the NodeJS Buffer
class that holds the encoded version of the Packet object you supplied as a argument to the function.
import net from 'net';
import { encodePacket, calculatePacketSize } from 'frostbite-rcon-utils';
let packet = {
sequence: 0,
isFromServer: false,
isResponse: false,
words: ['serverInfo']
};
packet.size = calculatePacketSize(packet.words);
packet.totalWords = packet.words.length;
/**
* To reduce the amount of boilerplate code used to create a packet.
* You can use the built-in function createPacket.
* createPacket(0, // sequence
* false, // isFromServer
* false, // isResponse
* ['serverInfo'] // words
*);
*/
let encodedPacket = encodePacket(packet);
let client = net.connect(47215, '188.126.64.4', () => {
client.write(encodedPacket);
});