configurable websocket keepalive ping
This commit is contained in:
parent
2da387c427
commit
448bd832a3
@ -31,10 +31,16 @@ export class IRCWebSocketConnector
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const onConnect = () => {
|
const onConnect = () => {
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
this.pingInterval = setInterval(() => {
|
|
||||||
this.lastPingName = Date.now().toString();
|
// Allow using a custom pinger, otherwise, ping every minute
|
||||||
this.write('PING :%s', this.lastPingName);
|
// to keep socket alive
|
||||||
}, 60000);
|
if (!this.connOpts?.skipPings) {
|
||||||
|
this.pingInterval = setInterval(() => {
|
||||||
|
this.lastPingName = Date.now().toString();
|
||||||
|
this.write('PING :%s', this.lastPingName);
|
||||||
|
}, (this.connOpts?.wsPingInterval as number) || 60000);
|
||||||
|
}
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,16 +70,16 @@ export class IRCWebSocketConnector
|
|||||||
this.socket?.addEventListener('message', (event) => {
|
this.socket?.addEventListener('message', (event) => {
|
||||||
const line = event.data.toString();
|
const line = event.data.toString();
|
||||||
|
|
||||||
if (line.indexOf('PING') === 0 && !this.connOpts?.skipPings) {
|
// Allow using a custom pinger
|
||||||
this.socket?.send('PONG' + line.substring(4));
|
if (!this.connOpts?.skipPings) {
|
||||||
return;
|
if (line.indexOf('PING') === 0) {
|
||||||
}
|
this.socket?.send('PONG' + line.substring(4));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (line.includes('PONG') && line.includes(' :' + this.lastPingName)) {
|
||||||
line.indexOf('PONG') === 0 &&
|
return;
|
||||||
line.includes(' :' + this.lastPingName)
|
}
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emit('data', line);
|
this.emit('data', line);
|
||||||
|
@ -144,6 +144,10 @@ export interface IIRCOptions {
|
|||||||
* Special cases for included connections:
|
* Special cases for included connections:
|
||||||
* - `path` - `IRCWebSocketConnector` will append this to the WebSocket URL.
|
* - `path` - `IRCWebSocketConnector` will append this to the WebSocket URL.
|
||||||
* - `skipPings` - Included connectors will not respond to PINGs if set.
|
* - `skipPings` - Included connectors will not respond to PINGs if set.
|
||||||
|
* - `wsPingInterval` - `IRCWebSocketConnector` pings the server instead of the server pinging it.
|
||||||
|
* Here, you can set the ping interval in milliseconds. Defaults to `60000`, 1 minute. This setting
|
||||||
|
* is ignored if you have specified `skipPings`, in which case, you will need to use a custom pinger
|
||||||
|
* for the WebSocket.
|
||||||
*/
|
*/
|
||||||
connOpts?: Record<string, unknown>;
|
connOpts?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user