pushrax / node-rcon

A generic RCON protocol client for node.js
MIT License
133 stars 31 forks source link

Doesn't seem to disconnect? #46

Open eX-C0n opened 1 year ago

eX-C0n commented 1 year ago

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 :". 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)
    }
pushrax commented 1 year ago

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