Skip to content

Commit

Permalink
New function WebSocketAcceptor.ping()
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Jul 29, 2024
1 parent 1482c9c commit 5966bb8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tgrid",
"version": "1.0.2",
"version": "1.0.3",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"exports": {
Expand Down
28 changes: 28 additions & 0 deletions src/protocols/web/WebSocketAcceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type http from "http";
import { sleep_for } from "tstl";
import type WebSocket from "ws";

import { Invoke } from "../../components/Invoke";
Expand Down Expand Up @@ -240,6 +241,33 @@ export class WebSocketAcceptor<
/* ----------------------------------------------------------------
COMMUNICATOR
---------------------------------------------------------------- */
/**
* Ping to the remote client.
*
* Send a ping message to the remote client repeatedly.
*
* The ping message would be sent every internal milliseconds, until the
* connection be disconnectedd. The remote client will reply with a pong
* message, so that the connection would be alive until be explicitly
* disconnected.
*
* @param ms Interval milliseconds
* @throws Error when the connection is not accepted.
*/
public ping(ms: number): void {
// TEST CONDITION
const error: Error | null = this.inspectReady("close");
if (error) throw error;
(async (): Promise<void> => {
while (this.state_ === WebSocketAcceptor.State.OPEN) {
await sleep_for(ms);
try {
this.socket_.ping();
} catch {}
}
})().catch(() => {});
}

/**
* @hidden
*/
Expand Down

0 comments on commit 5966bb8

Please sign in to comment.