diff --git a/components/app.js b/components/app.js index 43ea71a..46d6ae4 100644 --- a/components/app.js +++ b/components/app.js @@ -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); } } diff --git a/lib/client.js b/lib/client.js index 1f7c0ba..ffe6dfe 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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) {