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

good way to reconnect after connection lost with turntable? #198

Closed samuri51 closed 11 years ago

samuri51 commented 11 years ago

i'm wondering what a good way to reconnect after losing connection would be. I noticed that a lot of times my app will still be running on its server but the bot is not actually in the room... i've tried something like monitoring the time between speak events and then if nothing is recieved it tries to speak and if that fails it tries to reconnect. which works when i'm actually connected to turntable, but when i unhook the internet cable it seems to just stop trying to use the ttapi all together.

heres what i've tried...

I've also noticed that it doesn't throw an error is bot.connect doesn't work...

var checkActivity = Date.now(); // intializes timer

var checkIfConnected = function ()
{
    var currentActivity = (Date.now() - checkActivity) / 1000 / 60;

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

setInterval(checkIfConnected, 5000);

bot.on('speak', function (data)
{
    checkActivity = Date.now(); //update when someone says something
});
samuri51 commented 11 years ago

or is there some event i can listen for if my connection gets terminated? an error message or a timeout with bot.connect() ? i guess the above method wouldn't work though because if your not connected to the internet you can't recieve a callback right?

MikeWills commented 11 years ago

@dubbyTT said he had something working. I'd like to see what he figured out.

technobly commented 11 years ago

Half of me really wants to share my solution and help the community, while the other half wants to overtake TT.fm with B^Dub's Bots... $_$ lol. Pipe dream I guess... lemme spend some time making a proper fork of the TTAPI with my changes... and then I'll make an auto reconnecting bot example, since that is pretty important.

As far as crash recovery, there are a number of process monitors out there... but they are mostly bloated and crash themselves after a while (like "Forever", omg why are are there SO MANY FILES in the Forever installation!!!!??). Seriously , I just ended up coding my own and it works. I'm still beta testing it though, so no example for that yet.

MikeWills commented 11 years ago

That'd be great. I am using Forever and while it works... I know there has to be something better.

samuri51 commented 11 years ago

@DubbyTT thx man :), lookin forward to it

ghost commented 11 years ago

^remember to close once the problem is solved.

technobly commented 11 years ago

Yes thank you we got this @Turntablelover ... now get back to Codecademy.com and finish your voting command :smiley_cat:

technobly commented 11 years ago

@MikeWills @samuri51 This should solve your issues with auto reconnecting https://github.com/alaingilbert/Turntable-API/pull/223

Enjoy ;-)