Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

add rough reconnection support #541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 66 additions & 57 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeframe in this message doesn't match the setTimeout below -- maybe extract a variable?

});
}
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) {
Expand Down