Unregister PING timer when destroying client

This commit is contained in:
Simon Ser 2021-05-28 09:58:06 +02:00
parent 24fe62f1de
commit 7b762b3e8a
2 changed files with 20 additions and 5 deletions

View file

@ -548,11 +548,8 @@ export default class App extends Component {
this.switchToChannel = params.autojoin[0];
}
if (this.config.server && this.config.server.ping > 0) {
// TODO: unregister setInterval on disconnect
setInterval(() => {
client.send({ command: "PING", params: ["gamja"] });
}, this.config.server.ping * 1000);
if (this.config.server && typeof this.config.server.ping === "number") {
client.setPingInterval(this.config.server.ping);
}
}

View file

@ -46,6 +46,7 @@ export default class Client extends EventTarget {
batches = new Map();
autoReconnect = true;
reconnectTimeoutID = null;
pingIntervalID = null;
pendingHistory = Promise.resolve(null);
cm = irc.CaseMapping.RFC1459;
whoisDB = new irc.CaseMapMap(null, irc.CaseMapping.RFC1459);
@ -111,6 +112,8 @@ export default class Client extends EventTarget {
clearTimeout(this.reconnectTimeoutID);
this.reconnectTimeoutID = null;
this.setPingInterval(0);
if (this.ws) {
this.ws.close(1000);
}
@ -444,6 +447,21 @@ export default class Client extends EventTarget {
return this.cm(nick) == this.cm(this.nick);
}
setPingInterval(sec) {
clearInterval(this.pingIntervalID);
this.pingIntervalID = null;
if (sec <= 0) {
return;
}
this.pingIntervalID = setInterval(() => {
if (this.ws) {
this.send({ command: "PING", params: ["gamja"] });
}
}, sec * 1000);
}
/* Execute a command that expects a response. `done` is called with message
* events until it returns a truthy value. */
roundtrip(msg, done) {