janispritzkau / rcon-client

A simple and modern RCON client made to work with Minecraft
64 stars 17 forks source link

Error during connect leaves the client in broken state #8

Closed Hornwitser closed 4 years ago

Hornwitser commented 4 years ago

If an error occurs during the connection of the socket then no close event is sent which means this.socket is not cleared and connect() errors out with "Already connected or connecting" when called a second time. See code below for reproduction.

Rcon = require("rcon-client").Rcon;

async function bug() {
    let client = new Rcon({
        host: "localhost",
        port: 1234,
        password: "blah",
    });

    client.on("error", () => { /* ignore */ });

    try {
        await client.connect();
    } catch (err) {
        console.log("first error:", err.message);
    }

    try {
        await client.connect();
    } catch (err) {
        console.log("second error:", err.message);
    }
}

bug().catch(console.log);
// Expected output
// first error: connect ECONNREFUSED 127.0.0.1:1234
// second error: connect ECONNREFUSED 127.0.0.1:1234

// Actual output
// first error: connect ECONNREFUSED 127.0.0.1:1234
// second error: Already connected or connecting