Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function WebSocketAcceptor.ping() #86

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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