0

I'm trying to setup a rcon function, and I'm using node-rcon . With the example provided it works perfectly, but with my code, it doesn't work, I can't figure out why. Any clue what's the issue with my code? It throws no errors, but the server doesn't recieve the rcon command. Here's my code:

var Rcon = require('../node-rcon');
console.log('Starting the rcon...');
var conn = new Rcon('localhost', 30120, 'test123', options);
conn.on('auth', function() {
  console.log("Authed!");

}).on('response', function(str) {
  console.log("Got response: " + str);

}).on('end', function() {
  console.log("Socket closed!");
  process.exit();
});
conn.connect();
conn.send("say working!");
conn.disconnect();

It's a nodejs, and yes, the example works for me just fine.

3 Answers3

1

I copied your code here and make some changes to work. I hope this helps you.

First, you have to import the correct module after the installation. Second, you have to put your configuration options.

var Rcon = require('rcon');
console.log('Starting the rcon...');
var options = {
  tcp: false,       // false for UDP, true for TCP (default true)
  challenge: false  // true to use the challenge protocol (default true)
};
var conn = new Rcon('localhost', 30120, 'test123', options);
conn.on('auth', function() {
  console.log("Authed!");

}).on('response', function(str) {
  console.log("Got response: " + str);

}).on('end', function() {
  console.log("Socket closed!");
  process.exit();
});
conn.connect();
conn.send("say working!");
conn.disconnect();
  • Thank you for the answer. I forgot to post my options, and the import is correct, since I have a node-rcon.js file on my root. But it's still not working, and I can't figure out why. Your code is the same as mine, but with the options added that I forgot. THank you for the help anyway – pedroribeiro98 Apr 25 '20 at 15:03
0

The fix is actually easy, it's just a matter of waiting. This isn't a perfect solution, but it works for what I need.

async function sendmessage(frase){
  var conn = new Rcon('IP', 30120, 'PASSWORD', options);
  const sleep = ms => new Promise(res => setTimeout(res, ms));
  conn.connect();
  console.log('Waiting 1 sec...');
  await sleep(500);
  conn.send(frase);
  console.log('Waiting 1 sec...');
  await sleep(1000);
  conn.disconnect();
  return;

Hope this helps someone

0

I used the respond event from the first command to send the next one

var Rcon = require('rcon');
console.log('Starting the rcon...');
var conn = new Rcon('localhost', 30120, 'test123', options);
var options = {
  tcp: false,       // false for UDP, true for TCP (default true)
  challenge: false  // true to use the challenge protocol (default true)
};
queuedCommands = ['command1', 'command2', 'command3']
var i = 0;
function sendnext (i) {
  if(i === queuedCommands.length){
    conn.disconnect();
    return;
  }
  conn.send(queuedCommands[i]);
  return ++i;
}
conn.on('auth', function() {
  console.log("Rcon connection successfull");
  i = sendnext(i);// to send the first command
}).on('response', function(str) {
  console.log(str)
  i = sendnext(i);
}).on('error', function(err) {
  console.log("Error: " + err);
  output.push(err);
});
conn.connect();