Skip to content

Commit d979d91

Browse files
authored
Merge pull request #86 from samchon/feature/ping
New function `WebSocketAcceptor.ping()`
2 parents 1482c9c + 5966bb8 commit d979d91

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tgrid",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"main": "lib/index.js",
55
"typings": "lib/index.d.ts",
66
"exports": {

src/protocols/web/WebSocketAcceptor.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type http from "http";
2+
import { sleep_for } from "tstl";
23
import type WebSocket from "ws";
34

45
import { Invoke } from "../../components/Invoke";
@@ -240,6 +241,33 @@ export class WebSocketAcceptor<
240241
/* ----------------------------------------------------------------
241242
COMMUNICATOR
242243
---------------------------------------------------------------- */
244+
/**
245+
* Ping to the remote client.
246+
*
247+
* Send a ping message to the remote client repeatedly.
248+
*
249+
* The ping message would be sent every internal milliseconds, until the
250+
* connection be disconnectedd. The remote client will reply with a pong
251+
* message, so that the connection would be alive until be explicitly
252+
* disconnected.
253+
*
254+
* @param ms Interval milliseconds
255+
* @throws Error when the connection is not accepted.
256+
*/
257+
public ping(ms: number): void {
258+
// TEST CONDITION
259+
const error: Error | null = this.inspectReady("close");
260+
if (error) throw error;
261+
(async (): Promise<void> => {
262+
while (this.state_ === WebSocketAcceptor.State.OPEN) {
263+
await sleep_for(ms);
264+
try {
265+
this.socket_.ping();
266+
} catch {}
267+
}
268+
})().catch(() => {});
269+
}
270+
243271
/**
244272
* @hidden
245273
*/

0 commit comments

Comments
 (0)