From b1d47496fa8f7c9883b2b2ffc0397e46cc47c32a Mon Sep 17 00:00:00 2001 From: Daniel Llewellyn Date: Wed, 18 Nov 2015 02:43:02 +0000 Subject: [PATCH] add very rough reconnection support. - this breaks currently when your nickname is already in use and I'm unsure why. --- src/client.js | 123 +++++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/src/client.js b/src/client.js index 0a33ef95..068c792f 100644 --- a/src/client.js +++ b/src/client.js @@ -137,35 +137,10 @@ Client.prototype.connect = function(args) { } } - var stream = args.tls ? tls.connect(server) : net.connect(server); - - stream.on("error", function(e) { - console.log("Client#connect():\n" + e); - stream.end(); - var msg = new Msg({ - type: Msg.Type.ERROR, - text: "Connection error." - }); - client.emit("msg", { - msg: msg - }); - }); - var nick = args.nick || "shout-user"; var username = args.username || nick.replace(/[^a-zA-Z0-9]/g, ""); var realname = args.realname || "Shout User"; - var irc = slate(stream); - identd.hook(stream, username); - - if (args.password) { - irc.pass(args.password); - } - - irc.me = nick; - irc.nick(nick); - irc.user(username, realname); - var network = new Network({ name: server.name, host: server.host, @@ -177,47 +152,81 @@ Client.prototype.connect = function(args) { commands: args.commands }); - network.irc = irc; - client.networks.push(network); client.emit("network", { network: network }); - events.forEach(function(plugin) { - var path = "./plugins/irc-events/" + plugin; - require(path).apply(client, [ - irc, - network - ]); - }); + var reconnect = function(args) { + network = _.find(client.networks, {id: network.id}); + + var stream = args.tls ? tls.connect(server) : net.connect(server); - irc.once("welcome", function() { - var delay = 1000; - var commands = args.commands; - if (Array.isArray(commands)) { - commands.forEach(function(cmd) { - setTimeout(function() { - client.input({ - target: network.channels[0].id, - text: cmd - }); - }, delay); - delay += 1000; + stream.on("error", function(e) { + console.log("Client#connect():\n" + e); + stream.end(); + var msg = new Msg({ + type: Msg.Type.ERROR, + text: "Connection error. Reconnecting in 30 seconds." }); - } - setTimeout(function() { - irc.write("PING " + network.host); - }, delay); - }); + client.emit("msg", { + msg: msg + }); + setTimeout(function() { + reconnect(args); + }, 5000); + }); + + var irc = slate(stream); + identd.hook(stream, username); - irc.once("pong", function() { - var join = (args.join || ""); - if (join) { - join = join.replace(/\,/g, " ").split(/\s+/g); - irc.join(join); + if (args.password) { + irc.pass(args.password); } - }); + + irc.me = nick; + irc.nick(nick); + irc.user(username, realname); + + network.irc = irc; + + events.forEach(function(plugin) { + var path = "./plugins/irc-events/" + plugin; + require(path).apply(client, [ + irc, + network + ]); + }); + + irc.once("welcome", function() { + var delay = 1000; + var commands = args.commands; + if (Array.isArray(commands)) { + commands.forEach(function(cmd) { + setTimeout(function() { + client.input({ + target: network.channels[0].id, + text: cmd + }); + }, delay); + delay += 1000; + }); + } + setTimeout(function() { + irc.write("PING " + network.host); + }, delay); + }); + + irc.once("pong", function() { + var join = (args.join || ""); + if (join) { + join = join.replace(/\,/g, " ").split(/\s+/g); + irc.join(join); + } + }); + }; + + reconnect(args); }; Client.prototype.input = function(data) {