websocket pinger

This commit is contained in:
Evert Prants 2022-09-25 11:20:40 +03:00
parent 64ca53b494
commit 76868287eb
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 26 additions and 8 deletions

View File

@ -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);
});
}

View File

@ -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',