pushrax / node-rcon

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

Feature Request: Add "timed out" parameter #29

Open R4to0 opened 4 years ago

R4to0 commented 4 years ago

Hello!

Been using your library with my Discord bot and works like a charm!

The only issue I'm getting is if I try to rcon a server that is down (or changing map for example), it doesn't report any kind of timed out (at least when using UDP because of how the protocol works) and I have to handle that by myself with a timer or something (haven't tried yet).

Since there's no param for this in the library, it would be nice to have one :)

Here's how I'm using. If the server is up, everything works as expected.


async SendMsgRcon(target, msg) {
    return new Promise((resolve, reject) => {
        // max rcon data we can send is 509
        const maxrcondatasize = 509;
        const basesize = 16 + target.rcon.length; // rcon challenge and rcon length
        const basecomm = `some command here `;
        let conn = new Rcon(target.serverip, target.serverport, target.rcon, { tcp: false });

        conn.on('auth', () => {
            //console.log("Authed!");
            conn.send(basecomm + `"${msg.slice(0, (maxrcondatasize - basesize - basecomm.length - 2))}"`);

        }).on('response', (str) => {
            //console.dir("Got response: " + str);
            conn.disconnect();

            // handle status
            const status = str.replace(/\n|\0/g, "");
            if (status == 'OK')
                resolve(true);
            else /*if(status == 'BADMSG')*/
                resolve(false);
        }).on('end', () => {
            console.log("Rcon socket closed!");
        }).on('error', (error) => {
            console.log("error");
            conn.disconnect();
            reject(null);
        });
        conn.connect();
    });
}```

Thank you for your time.