tmijs / tmi.js

💬 Javascript library for the Twitch Messaging Interface. (Twitch.tv)
https://tmijs.com
MIT License
1.55k stars 214 forks source link

Trying to join an unexisting channel doesnt let you join correct channels #163

Open shaaw opened 8 years ago

shaaw commented 8 years ago

Tittle is self explanatory.

I try to join a random channel that i know it doesnt exist to report that error on the app, but after an failed attemp to join a chat the next ones do not work even with correct ones, i have to completely reconect to the serve to make it work again. This is the log i get catching the error like in the wiki

client.join("channel").then(function(data) {
    // data returns [channel]
}).catch(function(err) {
    //
});
[14:59] info: Connected to server.
[14:59] info: Executing command: JOIN #poasjdfopasdf
No response from Twitch.
[15:00] info: Executing command: JOIN #shaawsc2
[15:00] info: Joined #shaawsc2
No response from Twitch.

It clearly says that i joined the channel, but the code goes to the reject part of the Promise

shaaw commented 8 years ago

i think i have where the problem is In lib/client.js

client.prototype._sendCommand = function _sendCommand(delay, channel, command, fn) {
    // Race promise against delay..
    return new Promise((resolve, reject) => {
        _.promiseDelay(delay).then(() => { reject("No response from Twitch."); });

        // Make sure the socket is opened..
        if (!_.isNull(this.ws) && this.ws.readyState !== 2 && this.ws.readyState !== 3) {
            // Executing a command on a channel..
            if (!_.isNull(channel)) {
                this.log.info(`[${_.channel(channel)}] Executing command: ${command}`);
                this.ws.send(`PRIVMSG ${_.channel(channel)} :${command}`);
            }

            // Executing a raw command..
            else {
                this.log.info(`Executing command: ${command}`);
                this.ws.send(command);
            }
            fn(resolve, reject);
        }

        // Disconnected from server..
        else { reject("Not connected to server."); }
    });
};

The command sent is raced against a timer, the first time the timer expires so it emits the error from timing out. The second time the command is sent too but this time its correct, but it still fires the reject part of the timer.

I dont know how to fix it but maybe it helps you out.

shaaw commented 8 years ago

Okei, i found the problem. If you comment the line 171 of events.js inside the function once //type = type + count; It works fine. The problem is that when you try to join a channel the event "_promiseJoin" gets stored. If the channel doesnt exist that listener never gets removed.

The next time someone tries to join another channel the listener gets stored as "_promiseJoin2". But the event emited is "_promiseJoin" when the system get the response from twitch, but that already timed out. But you actually joined the channel.

Schmoopiie commented 8 years ago

I'm investigating this, thanks!

Schmoopiie commented 8 years ago

I found the problem, will need to figure out how to fix it, when joining a channel that doesn't exist, Twitch is not returning anything, no error/notice.

The way our custom event listeners library works, we need to fire _promiseJoin to remove it from the queue, and it is not being fired because Twitch is not returning any messages.

shaaw commented 8 years ago

Yeah, you relay on the timeout to notice that you coudnt join the channel. I tried to remove the event if the timeout expires, but i dont fully understand they way the code works and it did some weird behavior.