kläglicher Versuch Reconnects zu fixen

This commit is contained in:
Flummi 2021-01-27 09:03:59 +01:00
parent 864f7c3591
commit f3ecb33a9d
3 changed files with 19 additions and 6 deletions

View File

@ -65,7 +65,9 @@ export default class irc extends EventEmitter {
return this; return this;
})(); })();
} }
connect() { connect(reconnect = false) {
if(reconnect)
this.socket = null;
this.socket = (this.options.ssl ? tls : net).connect({ this.socket = (this.options.ssl ? tls : net).connect({
host: this.options.host, host: this.options.host,
port: this.options.port, port: this.options.port,
@ -75,6 +77,7 @@ export default class irc extends EventEmitter {
this.send(`USER ${this.username} 0 * : ${this.realname}`); this.send(`USER ${this.username} 0 * : ${this.realname}`);
if (this.options.sasl) if (this.options.sasl)
this.send("CAP LS"); this.send("CAP LS");
this.emit("data", "[irc] connected!");
}); });
this.socket.setEncoding("utf-8"); this.socket.setEncoding("utf-8");
this.socket.on("data", msg => { this.socket.on("data", msg => {
@ -84,14 +87,21 @@ export default class irc extends EventEmitter {
this._cmd.get(cmd.command)(cmd); this._cmd.get(cmd.command)(cmd);
}) })
}); });
this.socket.on("end", () => {
this.connect(true);
return this.emit("data", ["error", "[irc] stream ended, reconnecting in progress"]);
});
} }
send(data) { send(data) {
this.socket.write(`${data}\n`); if(this.socket)
this.socket.write(`${data}\n`);
else
this.emit("data", ["info", `[irc] nope: ${data}`]);
} }
sendmsg(mode, recipient, msg) { sendmsg(mode, recipient, msg) {
msg = Array.isArray(msg) ? msg : msg.split(/\r?\n/); msg = Array.isArray(msg) ? msg : msg.split(/\r?\n/);
if (msg.length >= 5) if (msg.length >= 5)
return this.emit("data", ["error", "too many lines"]); return this.emit("data", ["error", "[irc] too many lines"]);
msg.forEach(e => this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", this.format(e.toString())) )); msg.forEach(e => this.send( msgmodes[mode].replace("{recipient}", recipient).replace("{msg}", this.format(e.toString())) ));
} }
parse(data, [a, ...b] = data.split(/ +:/), tmp = a.split(" ").concat(b)) { parse(data, [a, ...b] = data.split(/ +:/), tmp = a.split(" ").concat(b)) {
@ -148,7 +158,7 @@ export default class irc extends EventEmitter {
if (tmpuser.cached < ~~(Date.now() / 1000) - this._recachetime) if (tmpuser.cached < ~~(Date.now() / 1000) - this._recachetime)
whois.push(u); whois.push(u);
} }
this.emit("data", ["info", `whois > ${whois}`]); this.emit("data", ["info", `[irc] whois > ${whois}`]);
this.send(`WHOIS ${whois}`); this.send(`WHOIS ${whois}`);
} }
parsePrefix(prefix) { parsePrefix(prefix) {

View File

@ -30,7 +30,7 @@ export default bot => {
bot._cmd.set("318", msg => { // whois_end bot._cmd.set("318", msg => { // whois_end
let tmpuser = {}; let tmpuser = {};
bot.emit("data", ["info", `whois < ${msg.params[1]}`]); //bot.emit("data", ["info", `whois < ${msg.params[1]}`]);
if (bot.server.user.has(msg.params[1])) if (bot.server.user.has(msg.params[1]))
tmpuser = bot.server.user.get(msg.params[1]); tmpuser = bot.server.user.get(msg.params[1]);
tmpuser = { tmpuser = {

View File

@ -87,9 +87,12 @@ export default class tg extends EventEmitter {
this.emit("data", ["error", "tg timed out lol"]); this.emit("data", ["error", "tg timed out lol"]);
else if(err.type === "tg") else if(err.type === "tg")
this.emit("data", ["error", err.message]); this.emit("data", ["error", err.message]);
await this.connect();
} }
finally { finally {
setTimeout(async () => { await this.poll(); }, this.options.pollrate); setTimeout(async () => {
await this.poll();
}, this.options.pollrate);
} }
} }
async send(chatid, msg, reply = null) { async send(chatid, msg, reply = null) {