Skip to content

Commit

Permalink
Reconnection support.
Browse files Browse the repository at this point in the history
  • Loading branch information
bews committed May 10, 2016
1 parent 90a62c5 commit c7928c2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 58 deletions.
132 changes: 74 additions & 58 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var events = [
"quit",
"topic",
"welcome",
"whois"
"whois",
"ping"
];
var inputs = [
"action",
Expand Down Expand Up @@ -138,35 +139,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,
Expand All @@ -178,47 +154,87 @@ 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});

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;
var stream = args.tls ? tls.connect(server) : net.connect(server);

stream.on("error", function(e) {
console.log("Client#connect():\n" + e);
if (e.message.indexOf("write after end") !== -1) {
return; // fix exponential reconnection attempts
}
stream.end();
var msg = new Msg({
type: Msg.Type.ERROR,
text: "Connection error. Reconnecting in 15 seconds."
});
}
setTimeout(function() {
irc.write("PING " + network.host);
}, delay);
});
var lobby = network.channels[0];
lobby.messages.push(msg);
client.emit("msg", {
chan: lobby.id,
msg: msg
});
setTimeout(function() {
reconnect(args);
}, 15000);
});

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) {
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/irc-events/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function(irc, network) {
var ping_check = function(irc) {
console.log("Client#PING timeout check");
irc.write("PING " + network.host); // connection is lost, send PING to confirm it and trigger error
};

irc.ptimer = setTimeout(ping_check, 300000, irc);

irc.on("data", function(data) {
if ("PING" === data.command) {
clearTimeout(irc.ptimer);
irc.ptimer = setTimeout(ping_check, 300000, irc);
}
});
};

0 comments on commit c7928c2

Please sign in to comment.