~emersion/gamja

266ab2b0970316c5b204c14d82d75849e0fa4d96 — Simon Ser 1 year, 5 months ago 3a95fd5 ping
lib/client: add ping()
1 files changed, 12 insertions(+), 1 deletions(-)

M lib/client.js
M lib/client.js => lib/client.js +12 -1
@@ 51,6 51,7 @@ const WHOX_FIELDS = {

let lastLabel = 0;
let lastWhoxToken = 0;
let lastPingSerial = 0;

class IRCError extends Error {
	constructor(msg) {


@@ 746,7 747,7 @@ export default class Client extends EventTarget {

		this.pingIntervalID = setInterval(() => {
			if (this.ws) {
				this.send({ command: "PING", params: ["gamja"] });
				this.ping();
			}
		}, sec * 1000);
	}


@@ 954,6 955,16 @@ export default class Client extends EventTarget {
		return networks;
	}

	async ping() {
		lastPingSerial++;
		let token = "gamja-" + lastPingSerial;

		let msg = { command: "PING", params: [token] };
		await this.roundtrip(msg, (reply) => {
			return reply.command === "PONG" && reply.params[1] === token;
		});
	}

	monitor(target) {
		if (this.monitored.has(target)) {
			return;