mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 21:16:09 +00:00
feat: shard#ping method
This commit is contained in:
parent
c409628a43
commit
5338248ff8
@ -1,3 +1,4 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import NodeWebSocket from 'ws';
|
||||
|
||||
export class BaseSocket {
|
||||
@ -32,6 +33,21 @@ export class BaseSocket {
|
||||
return this.internal.close(...args);
|
||||
}
|
||||
|
||||
async ping() {
|
||||
if (!('ping' in this.internal)) throw new Error('Unexpected: Method ping not implemented');
|
||||
return new Promise<number>(res => {
|
||||
const nonce = randomUUID();
|
||||
const start = performance.now();
|
||||
const listener = (data: Buffer) => {
|
||||
if (data.toString() !== nonce) return;
|
||||
(this.internal as NodeWebSocket).removeListener('pong', listener);
|
||||
res(performance.now() - start);
|
||||
};
|
||||
(this.internal as NodeWebSocket).on('pong', listener);
|
||||
(this.internal as NodeWebSocket).ping(nonce);
|
||||
});
|
||||
}
|
||||
|
||||
get readyState() {
|
||||
return this.internal.readyState;
|
||||
}
|
||||
|
@ -74,6 +74,11 @@ export class Shard {
|
||||
return url.href;
|
||||
}
|
||||
|
||||
ping() {
|
||||
if (!this.websocket) return Promise.resolve(Number.POSITIVE_INFINITY);
|
||||
return this.websocket.ping();
|
||||
}
|
||||
|
||||
async connect() {
|
||||
await this.connectTimeout.wait();
|
||||
if (this.isOpen) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user