From 8717f02da3a2d356ea818b622af1f6147b4248d5 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 20 Sep 2011 16:20:36 +0100 Subject: [PATCH] Connection handling revamp, auto-reconnect --- css/ui.css | 1 + js/front.js | 58 +++++++++++++++++++++++++++++++++---- js/gateway.js | 45 +++++++++++++++++++++++----- node/client/index.html.jade | 1 + 4 files changed, 92 insertions(+), 13 deletions(-) diff --git a/css/ui.css b/css/ui.css index c13d572..1f15d5b 100644 --- a/css/ui.css +++ b/css/ui.css @@ -72,6 +72,7 @@ #kiwi #login .content.bottom a { display:block; text-align:right; font-size:16px; margin-top:30px; } +#kiwi .newnick .caption { display:block; margin-bottom:1em; } #kiwi .about { top: 100px; diff --git a/js/front.js b/js/front.js index 0559176..2f7160f 100644 --- a/js/front.js +++ b/js/front.js @@ -30,6 +30,7 @@ kiwi.front = { $(kiwi.gateway).bind("onconnect", kiwi.front.onConnect); $(kiwi.gateway).bind("onconnect_fail", kiwi.front.onConnectFail); $(kiwi.gateway).bind("ondisconnect", kiwi.front.onDisconnect); + $(kiwi.gateway).bind("onreconnecting", kiwi.front.onReconnecting); $(kiwi.gateway).bind("onnick", kiwi.front.onNick); $(kiwi.gateway).bind("onuserlist", kiwi.front.onUserList); $(kiwi.gateway).bind("onuserlist_end", kiwi.front.onUserListEnd); @@ -523,7 +524,26 @@ kiwi.front = { }, onConnect: function (e, data) { + var err_box, channels, tabview, i; + if (data.connected) { + // Did we disconnect? + err_box = $('.messages .msg.error.disconnect .text'); + if (typeof err_box[0] !== 'undefined') { + err_box.text('Reconnected OK :)'); + err_box.removeClass('disconnect'); + + // Rejoin channels + channels = ''; + $.each(kiwi.front.tabviews, function (i, tabview) { + if (tabview.name == 'server') return; + channels += tabview.name + ','; + }); + console.log('Rejoining: ' + channels); + kiwi.gateway.join(channels); + return; + } + if (kiwi.gateway.nick !== data.nick) { kiwi.gateway.nick = data.nick; kiwi.front.doLayout(); @@ -547,10 +567,33 @@ kiwi.front = { onDisconnect: function (e, data) { var tab; for (tab in kiwi.front.tabviews) { - kiwi.front.tabviews[tab].addMsg(null, '', 'Disconnected from server!', 'error'); + kiwi.front.tabviews[tab].addMsg(null, '', 'Disconnected from server!', 'error disconnect'); } kiwi.plugs.run('disconnect', {success: false}); }, + onReconnecting: function (e, data) { + var err_box, f, msg; + + err_box = $('.messages .msg.error.disconnect .text'); + if (!err_box) return; + + f = function (num) { + switch (num) { + case 1: return 'First'; + case 2: return 'Second'; + case 3: return 'Third'; + case 4: return 'Fourth'; + case 5: return 'Fifth'; + case 6: return 'Sixth'; + case 7: return 'Seventh'; + default: return 'Next' + } + }; + + // TODO: convert seconds to mins:secs + msg = f(data.attempts) + ' attempt at reconnecting in ' + (data.delay / 1000).toString() + ' seconds..'; + err_box.text(msg); + }, onOptions: function (e, data) { if (typeof kiwi.gateway.network_name === "string" && kiwi.gateway.network_name !== "") { kiwi.front.tabviews.server.tab.text(kiwi.gateway.network_name); @@ -694,10 +737,12 @@ kiwi.front = { kiwi.front.tabviewAdd(data.channel.toLowerCase()); } + kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> ' + data.nick + ' has joined', 'action join', 'color:#009900;'); + if (data.nick === kiwi.gateway.nick) { return; // Not needed as it's already in nicklist } - kiwi.front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> ' + data.nick + ' has joined', 'action join', 'color:#009900;'); + $('
  • ' + data.nick + '
  • ').appendTo(kiwi.front.tabviews[data.channel.toLowerCase()].userlist); kiwi.front.tabviews[data.channel.toLowerCase()].userlistSort(); }, @@ -808,7 +853,7 @@ kiwi.front = { break; case 'nickname_in_use': kiwi.front.tabviews.server.addMsg(null, ' ', '=== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status'); - kiwi.front.showChangeNick(); + kiwi.front.showChangeNick('That nick is already taken'); break; default: // We don't know what data contains, so don't do anything with it. @@ -1023,9 +1068,13 @@ kiwi.front = { }, - showChangeNick: function () { + showChangeNick: function (caption) { + caption = (typeof caption !== 'undefined') ? caption : ''; + $('#kiwi').append($('#tmpl_change_nick').tmpl({})); + $('#kiwi .newnick .caption').text(caption); + $('#kiwi .form_newnick').submit(function () { kiwi.front.run('/NICK ' + $('#kiwi .txtnewnick').val()); $('#kiwi .newnick').remove(); @@ -1651,7 +1700,6 @@ Tabview.prototype.addMsg = function (time, nick, msg, type, style) { Tabview.prototype.scrollBottom = function () { var panel = this.panel; - console.log(panel); panel[0].scrollTop = panel[0].scrollHeight; }; diff --git a/js/gateway.js b/js/gateway.js index b8719bb..f181fcb 100644 --- a/js/gateway.js +++ b/js/gateway.js @@ -21,33 +21,62 @@ kiwi.gateway = { connect: function (host, port, ssl, callback) { if (typeof kiwi.gateway.kiwi_server !== 'undefined') { - kiwi.gateway.socket = io.connect(kiwi_server, {'max reconnection attempts': 3}); - kiwi.gateway.socket.of('/kiwi').on('connect_failed', function (reason) { + kiwi.gateway.socket = io.connect(kiwi_server, { + 'try multiple transports': true, + 'connect timeout': 3000, + 'max reconnection attempts': 7, + 'reconnection delay': 2000, + + }); + kiwi.gateway.socket.on('connect_failed', function (reason) { // TODO: When does this even actually get fired? I can't find a case! ~Darren console.debug('Unable to connect Socket.IO', reason); + console.log("kiwi.gateway.socket.on('connect_failed')"); //kiwi.front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error'); kiwi.gateway.socket.disconnect(); $(kiwi.gateway).trigger("onconnect_fail", {reason: reason}); kiwi.gateway.sendData = function () {}; }).on('error', function (e) { $(kiwi.gateway).trigger("onconnect_fail", {reason: e}); - console.debug(e); + console.log("kiwi.gateway.socket.on('error')"); console.log(e); }); + + kiwi.gateway.socket.on('connecting', function (transport_type) { + console.log("kiwi.gateway.socket.on('connecting')"); + $(kiwi.gateway).trigger("connecting"); + }); + kiwi.gateway.socket.on('connect', function () { + // This is also called when reconnected.. kiwi.gateway.sendData = function (data, callback) { kiwi.gateway.socket.emit('message', {sid: this.session_id, data: $.toJSON(data)}, callback); }; - kiwi.gateway.socket.on('message', kiwi.gateway.parse); - kiwi.gateway.socket.on('disconnect', function () { - // Teardown procedure here - $(kiwi.gateway).trigger("ondisconnect", {}); - }); + kiwi.gateway.socket.emit('irc connect', kiwi.gateway.nick, host, port, ssl, callback); + console.log("kiwi.gateway.socket.on('connect')"); }); kiwi.gateway.socket.on('too_many_connections', function () { $(kiwi.gateway).trigger("onconnect_fail", {reason: 'too_many_connections'}); }); + + kiwi.gateway.socket.on('message', kiwi.gateway.parse); + kiwi.gateway.socket.on('disconnect', function () { + // Teardown procedure here + $(kiwi.gateway).trigger("ondisconnect", {}); + console.log("kiwi.gateway.socket.on('disconnect')"); + }); + kiwi.gateway.socket.on('close', function () { + console.log("kiwi.gateway.socket.on('close')"); + }); + + kiwi.gateway.socket.on('reconnecting', function (reconnectionDelay, reconnectionAttempts) { + console.log("kiwi.gateway.socket.on('reconnecting')"); + $(kiwi.gateway).trigger("onreconnecting", {delay: reconnectionDelay, attempts: reconnectionAttempts}); + }); + kiwi.gateway.socket.on('reconnect_failed', function () { + console.log("kiwi.gateway.socket.on('reconnect_failed')"); + }); } }, diff --git a/node/client/index.html.jade b/node/client/index.html.jade index f4083f8..6d296f7 100644 --- a/node/client/index.html.jade +++ b/node/client/index.html.jade @@ -85,6 +85,7 @@ html(xmlns="http://www.w3.org/1999/xhtml", lang="en-gb") script#tmpl_change_nick(type="text/x-jquery-tmpl") |
    + | | Your new nickname:
    |
    |
    -- 2.25.1