alaingilbert / Turntable-API

Allows you to create bots for turntable.fm
http://alaingilbert.github.com/Turntable-API/
MIT License
318 stars 97 forks source link

reconnection code fails after some time #270

Closed samuri51 closed 3 years ago

samuri51 commented 3 years ago

posting this here because i'm not sure where it belongs....

I currently use this code to have my bot to reconnect it's room on disconnect

var checkActivity = Date.now();

bot.on('disconnected', function (data) {

});

var checkIfConnected = function ()
{
    if (attemptToReconnect === null) //if a reconnection attempt is already in progress, do not attempt it
    {
        if (bot._isAuthenticated) // if bot is actually connected to turntable use the speaking method
        {
            var currentActivity = (Date.now() - checkActivity) / 1000 / 60;

            if (currentActivity > 30) //if greater than 30 minutes of no talking
            {
                bot.speak('ping', function (callback) //attempt to talk
                    {
                        if (callback.success === false) //if it fails
                        {
                            attempToReconnect();
                        }
                    });
            }
        }
        else //else attempt to reconnect right away
        {
            attempToReconnect();
        }
    }
};

setInterval(checkIfConnected, 5000);

//makes the bot attempt to reconnect to the room

function attempToReconnect()
{
    attemptToReconnect = setInterval(function ()
    {
        if (bot._isAuthenticated)
        {
            whichMessage = true;
            console.log('it looks like your bot is not in it\'s room. attempting to reconnect now....');
        }
        else
        {
            whichMessage = false;
            console.log('connection with turntable lost, waiting for connection to come back...');
        }

        bot.roomRegister(ROOMID, function (data)
        {
            if (data.success === true)
            {
                errorMessage = null;
                clearInterval(attemptToReconnect);
                attemptToReconnect = null;
                checkActivity = Date.now();

                if (whichMessage)
                {
                    console.log('the bot has reconnected to the room ' +
                        'specified by your choosen roomid');
                }
                else
                {
                    console.log('connection with turntable is back!');
                }
            }
            else
            {
                if (errorMessage === null && typeof data.err === 'string')
                {
                    errorMessage = data.err;
                }
            }
        });
    }, 1000 * 10);
};

//checks when the bot speaks
bot.on('speak', function (data)
{
checkActivity = Date.now(); //update when someone says something   
});

Assumptions about it: There are two types of disconnects, ones where you are authenticated and ones where you aren't.

not authenticated: not in the room so reconnect

authenticated: i use chatting activity for this. If i see no chatting for 30 minutes I may or may not be in the room. so I attempt to say "ping". if ping doesn't go through then i'm not in the room so reconnect.

it seems to work.... till it doesn't. after about 10-20 days the bot gets lost and doesn't respond to input anymore without a hard reset, despite the fact that the program itself is still running.

any ideas on what might be happening? Thanks.

alaingilbert commented 3 years ago

Why did you close this ? did you find what was going on ? Not sure if it could be related to: image

samuri51 commented 3 years ago

didn't figure it out i just decided to try running it in debugger / repl mode to see if anything gets lost over time :). the code (should) let the bot come back if it gets booted to the lobby. someone told me this was happening on their own bot though so i'm not sure if it's the code, the api, or the website.

but i am trying to find out! it can take a couple weeks to see it happen so it might take me some time to gather any data haha.