node-rcon icon indicating copy to clipboard operation
node-rcon copied to clipboard

Doesn't seem to disconnect?

Open eX-C0n opened this issue 2 years ago • 1 comments

When I run this code via a discord command, it will successfully send the command to the server, and successfully send the response of the command! The .on('end',()) function fires off after exactly 1 hour. So, attempting to run another command, within that hour, consistently results in: "Error: connect ETIMEDOUT <serverIp>:". The only way I've found to get around this limitation is to reboot my discord bot via process.exit(). Once rebooted, it will work one time again.

Having that been said, I am currently forcing my bot to reboot every time this particular discord command is ran, so that I can use commands without having to manually reboot. (I've commented the process.exit() in the code below, as the below code is what is currently a 1-time-use command.

Is there a way to get around this? To limit the time the socket connection stays open? conn.disconnect(); does not disconnect the socket connection.

let Rcon = require('rcon')
    const conn = new Rcon(process.env.IP, process.env.PORT, process.env.PASSWORD);
    try {
      message.channel.send(`Please wait for Server Authentication:`)
      conn.connect();
      conn.on('auth', function () {
        // You must wait until this event is fired before sending any commands,
        // otherwise those commands will fail.
        message.channel.send(`Authenticated - Sending command: ${args.join(" ")}`);
        conn.send(args.join(" "));
      }).on('response', (str) => {
        message.channel.send("Response: " + str);
        //process.exit()
      }).on('end', () => {
        message.channel.send("Rcon socket closed!");
      }).on('error', (error) => {
        message.channel.send(`Error ${error}`)
        conn.disconnect();
      });
    } catch (error) {
      console.error(error)
    }

eX-C0n avatar Oct 06 '22 23:10 eX-C0n

It looks like the server-side socket is timing out since there's not much traffic on the connection. Two things you could try:

  • Disconnect the connection on response, and reconnect when you want to send another command. This makes sense if you're only sending commands once in a while.
  • Send a ping once in a while (e.g. every 10 seconds) to keep the connection alive. This makes sense if you're sending commands fairly frequently and want to ensure the lowest latency possible.

pushrax avatar Oct 10 '22 17:10 pushrax