Skip to content

Commit

Permalink
Update autoreconnect.js
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Jun 28, 2013
1 parent f97cb1f commit 65d0e1e
Showing 1 changed file with 47 additions and 162 deletions.
209 changes: 47 additions & 162 deletions examples/autoreconnect.js
@@ -1,166 +1,51 @@
// .d8888b. 888 888 d8888 88888888888 88888888888 Y88b d88P var Bot = require('ttapi');
// d88P Y88b 888 888 d88888 888 888 Y88b d88P var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxx';
// 888 888 888 888 d88P888 888 888 Y88o88P
// 888 8888888888 d88P 888 888 888 Y888P
// 888 888 888 d88P 888 888 888 888
// 888 888 888 888 d88P 888 888 888 888
// Y88b d88P 888 888 d8888888888 888 888 888
// "Y8888P" 888 888 d88P 888 888 888 888
//
// ChattyTT - Copyright (C) 2013 B^Dub - dubbytt@gmail.com - Last update June 15th 2013
// (subset of Chatty's 3000+ lines of code... auto reconnect code and uptime command)
//
// A Turntable.fm bot that automatically reconnects to tt.fm after
// the internet connection goes away and comes back, or tt.fm goes down
// for maintenance and comes back. Responds to /uptime command and will
// report uptime, how many times it has been down and how long the last time was.
//
// This software is best viewed with Sublime Text http://www.sublimetext.com
//
// ASCII GEN http://patorjk.com/software/taag/#p=display&f=Colossal&t=STALKBOT
//-----------------------------------------------------------------------------

var Bot = require('ttapi');
// FIGURE OUT YOUR AUTH, USERID, ROOMID with this tool:
// http://alaingilbert.github.io/Turntable-API/bookmarklet.html
var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var USERID = 'xxxxxxxxxxxxxxxxxxxxxxxx'; var USERID = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var ROOMID = 'xxxxxxxxxxxxxxxxxxxxxxxx'; var ROOMID = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var BOTNAME = 'ChattyTT';
var ADMIN = 'xxxxxxxxxxxxxxxxxxxxxxxx'; var bot = new Bot(AUTH, USERID);


var netwatchdogTimer = null; // Used to detect internet connection dropping out /*
var startTime = Date.now(); // Holds start time of the bot * We use a flag to track whether or not we are disconnected. This is because
var reLogins = 0; // The number of times the bot has re-logged on due to internet/tt.fm outage. * in most cases the underlying WebSocket class will emit 2 events when a
var botDownDATEtime = ""; // The time/date the bot went down. * connection error occurs: the first indicates an unexpected stream error,
var botDownUTCtime = 0; // Used to save the UTC time the bot went down. * and the second indicates that the connection is closed. Tracking the
var botDowntime = 0; // Used to save the duration of time the bot was down for last. * current state allows us to ensure that we are making only one attempt to

* recover from the connection failure.
var bot = new Bot(AUTH, USERID, ROOMID); */
// set this to 'true' to see lots and LOTS of debug data :-/ var disconnected = false;
bot.debug = false;

/*
// 8888888b. 8888888888 d8888 8888888b. Y88b d88P * Connect/reconnect to Turntable
// 888 Y88b 888 d88888 888 "Y88b Y88b d88P */
// 888 888 888 d88P888 888 888 Y88o88P function connect(roomid) {
// 888 d88P 8888888 d88P 888 888 888 Y888P // Reset the disconnected flag
// 8888888P" 888 d88P 888 888 888 888 disconnected = false;
// 888 T88b 888 d88P 888 888 888 888
// 888 T88b 888 d8888888888 888 .d88P 888 // Attempt to join the room
// 888 T88b 8888888888 d88P 888 8888888P" 888 bot.roomRegister(roomid, function (data) {
bot.on('ready', function () { if (data && data.success) {
console.log("[ " + BOTNAME + " is READY FREDDY! on " + Date() + " ] "); console.log('Joined ' + data.room.name);
}); } else {

console.log('Failed to join room');
// .d8888b. 8888888b. 8888888888 d8888 888 d8P if (!disconnected) {
// d88P Y88b 888 Y88b 888 d88888 888 d8P // Set the disconnected flag
// Y88b. 888 888 888 d88P888 888 d8P disconnected = true;
// "Y888b. 888 d88P 8888888 d88P 888 888d88K // Try again in 60 seconds
// "Y88b. 8888888P" 888 d88P 888 8888888b setTimeout(connect, 60 * 1000, roomid);
// "888 888 888 d88P 888 888 Y88b }
// Y88b d88P 888 888 d8888888888 888 Y88b }
// "Y8888P" 888 8888888888 d88P 888 888 Y88b });
bot.on('speak', function (data) {
//log chat to the console
console.log(data.name + ': ' + data.text);
data.text = data.text.trim(); //Get rid of any surrounding whitespace

// Respond to "uptime" command
if (data.text.match(/^\/uptime$/i)) {
upTime(data, false);
}
});

// 8888888b. 888b d888 888b d888 8888888888 8888888b.
// 888 Y88b 8888b d8888 8888b d8888 888 888 "Y88b
// 888 888 88888b.d88888 88888b.d88888 888 888 888
// 888 d88P 888Y88888P888 888Y88888P888 8888888 888 888
// 8888888P" 888 Y888P 888 888 Y888P 888 888 888 888
// 888 888 Y8P 888 888 Y8P 888 888 888 888
// 888 888 " 888 888 " 888 888 888 .d88P
// 888 888 888 888 888 8888888888 8888888P"
bot.on('pmmed', function (data) {
// Respond to "uptime" command
if (data.text.match(/^\/uptime$/i)) {
upTime(data, true);
}
});

// 8888888b. 8888888 .d8888b. .d8888b. .d88888b. 888b 888 888b 888 8888888888 .d8888b. 88888888888 8888888888 8888888b.
// 888 "Y88b 888 d88P Y88b d88P Y88b d88P" "Y88b 8888b 888 8888b 888 888 d88P Y88b 888 888 888 "Y88b
// 888 888 888 Y88b. 888 888 888 888 88888b 888 88888b 888 888 888 888 888 888 888 888
// 888 888 888 "Y888b. 888 888 888 888Y88b 888 888Y88b 888 8888888 888 888 8888888 888 888
// 888 888 888 "Y88b. 888 888 888 888 Y88b888 888 Y88b888 888 888 888 888 888 888
// 888 888 888 "888 888 888 888 888 888 Y88888 888 Y88888 888 888 888 888 888 888 888
// 888 .d88P 888 Y88b d88P Y88b d88P Y88b. .d88P 888 Y8888 888 Y8888 888 Y88b d88P 888 888 888 .d88P
// 8888888P" 8888888 "Y8888P" "Y8888P" "Y88888P" 888 Y888 888 Y888 8888888888 "Y8888P" 888 8888888888 8888888P"
bot.on('disconnected', function (data) { // Loss of connection detected, takes about 20 seconds
console.log("[ BOT WAS DISCONNECTED ]: " + data + " on " + Date());
botDownDATEtime = Date(); // save the down date/time.
botDownUTCtime = Date.now(); // save the UTC time the bot went down.
setTimeout(function () {
startWatchdog();
}, 10 * 1000); // give the bot 10 seconds to fully fail before attempting to reconnect
});

// d8888 888 8888888 888 888 8888888888
// d88888 888 888 888 888 888
// d88P888 888 888 888 888 888
// d88P 888 888 888 Y88b d88P 8888888
// d88P 888 888 888 Y88b d88P 888
// d88P 888 888 888 Y88o88P 888
// d8888888888 888 888 Y888P 888
// d88P 888 88888888 8888888 Y8P 8888888888
bot.on('alive', function () { // Reset the watchdog timer if bot is alive
if (netwatchdogTimer != null) {
clearTimeout(netwatchdogTimer);
netwatchdogTimer = null;
}
});

// 8888888888 888 888 888b 888 .d8888b. d8b
// 888 888 888 8888b 888 d88P Y88b 88P
// 888 888 888 88888b 888 888 888 8P
// 8888888 888 888 888Y88b 888 888 " .d8888b
// 888 888 888 888 Y88b888 888 88K
// 888 888 888 888 Y88888 888 888 "Y8888b.
// 888 Y88b. .d88P 888 Y8888 Y88b d88P X88
// 888 "Y88888P" 888 Y888 "Y8888P" 88888P'
function upTime(data, pm) {
var timeNow = Date.now();
var upTime = timeNow - startTime;
var utHours = Math.floor(upTime / (1000 * 3600));
var utMins = Math.floor((upTime % (3600 * 1000)) / (1000 * 60));
var utSecs = Math.floor((upTime % (60 * 1000)) / 1000);
if (reLogins > 0) var relogins = " and gracefully re-logged on due to internet / tt.fm outages " + reLogins + " time(s). Was last down for " + botDowntime + " second(s)";
else var relogins = "";
if (utHours > 0) {
if (pm) bot.pm("I've been slaving away for " + utHours + " hour(s) " + utMins + " minute(s) and " + utSecs + " second(s) now!" + relogins, data.senderid);
else bot.speak("/me has been slaving away for " + utHours + " hour(s) " + utMins + " minute(s) and " + utSecs + " second(s) now!" + relogins);
} else if (utMins > 0) {
if (pm) bot.pm("I've been slaving away for " + utMins + " minute(s) and " + utSecs + " second(s) now!" + relogins, data.senderid);
else bot.speak("/me has been slaving away for " + utMins + " minute(s) and " + utSecs + " second(s) now!" + relogins);
} else {
if (pm) bot.pm("I've been slaving away for " + utSecs + " second(s) now!" + relogins, data.senderid);
else bot.speak("/me has been slaving away for " + utSecs + " second(s) now!" + relogins);
}
} }


function startWatchdog() { // Start the watchdog timer bot.on('ready', function(data) { connect(ROOMID); });
if (netwatchdogTimer == null) {
netwatchdogTimer = setInterval(function () { bot.on('disconnected', function(e) {
console.log("[ WAITING FOR INTERNET/TT.FM TO COME BACK!!! ]"); if (!disconnected) {
bot.roomRegister(ROOMID, function (data) { // Set the disconnected flag and display message
if (data && data.success) { disconnected = true;
console.log("[ I'M BACK!!!! WEEEEEEEeeeeeeeeee!!! ]"); console.log("disconnected: " + e);
botDowntime = (Date.now() - botDownUTCtime) / 1000; // Attempt to reconnect in 10 seconds
reLogins += 1; // Increment the reLogin counter. setTimeout(connect, 10 * 1000, ROOMID);
bot.pm("NET/TT.FM WAS DOWN on " + botDownDATEtime + " for " + botDowntime + " second(s)", ADMIN);
console.log("[ NET/TT.FM WAS DOWN on " + botDownDATEtime + " for " + botDowntime + " second(s) ]");
// Here you can re-initialize things if you need to, like re-loading a queue
// ...
}
});
}, 10 * 1000); // Try to log back in every 10 seconds
} }
} });

0 comments on commit 65d0e1e

Please sign in to comment.