diff --git a/src/connector/websocket.connector.ts b/src/connector/websocket.connector.ts index 6ef957a..d72108d 100644 --- a/src/connector/websocket.connector.ts +++ b/src/connector/websocket.connector.ts @@ -9,6 +9,8 @@ export class IRCWebSocketConnector { connected = false; private socket?: WebSocket; + private pingInterval?: any; + private lastPingName?: string; constructor( public secure: boolean, @@ -29,6 +31,10 @@ export class IRCWebSocketConnector return new Promise((resolve, reject) => { const onConnect = () => { this.connected = true; + this.pingInterval = setInterval(() => { + this.lastPingName = Date.now().toString(); + this.write('PING :%s', this.lastPingName); + }, 60000); resolve(); }; @@ -47,6 +53,7 @@ export class IRCWebSocketConnector this.connected = false; this.socket?.close(); this.socket = undefined; + clearInterval(this.pingInterval); } write(format: string, ...args: any[]): void { @@ -62,6 +69,13 @@ export class IRCWebSocketConnector return; } + if ( + line.indexOf('PONG') === 0 && + line.includes(' :' + this.lastPingName) + ) { + return; + } + this.emit('data', line); }); @@ -69,6 +83,7 @@ export class IRCWebSocketConnector this.socket?.addEventListener('close', (err) => { this.connected = false; this.socket = undefined; + clearInterval(this.pingInterval); this.emit('close', err.reason); }); } diff --git a/src/irc.ts b/src/irc.ts index 83f98c7..e284840 100644 --- a/src/irc.ts +++ b/src/irc.ts @@ -296,7 +296,17 @@ export class IRCConnection this.emit('authenticated', true); // Send a whois request for self in order to reliably fetch hostname of self - this.write('WHOIS %s', this.options.nick); + this.useCollector( + new WhoisCollector((lines) => { + const wholine = lines.find(({ command }) => command === '311'); + if (wholine) { + this.options.hostname = wholine.arguments?.[3]; + console.log('self-whois hostname', this.options.hostname); + } + }, this.options.nick), + 'WHOIS %s', + this.options.nick, + ); } break; case '005': { @@ -338,13 +348,6 @@ export class IRCConnection case '396': this.options.hostname = line.arguments?.[1]; break; - // Set hostname from self-whois - case '311': // RPL_WHOISUSER - if (line.arguments?.[1] !== this.options.nick) { - return; - } - this.options.hostname = line.arguments?.[3]; - break; case '321': // RPL_LISTSTART this.emit('channel-list-item', { channel: 'Channel',