MCPcopy Create free account
hub / github.com/Dart-Code/Dart-Code / attachPing

Function attachPing

src/shared/utils/ws.ts:11–40  ·  view source on GitHub ↗
(socket: ws.WebSocket, pingInterval = 15000)

Source from the content-addressed store, hash-verified

9 * https://github.com/Dart-Code/Dart-Code/issues/5794
10 */
11export function attachPing(socket: ws.WebSocket, pingInterval = 15000) {
12 const startPinging = () => {
13 const timer = setInterval(() => {
14 if (socket.readyState === ws.WebSocket.OPEN) {
15 try {
16 socket.ping();
17 } catch {
18 clear();
19 }
20 } else {
21 clear();
22 }
23 }, pingInterval).unref();
24
25 const clear = () => {
26 clearInterval(timer);
27 socket.removeListener("close", clear);
28 socket.removeListener("error", clear);
29 };
30
31 socket.on("close", clear);
32 socket.on("error", clear);
33 };
34
35 if (socket.readyState === ws.WebSocket.OPEN) {
36 startPinging();
37 } else {
38 socket.once("open", startPinging);
39 }
40}

Callers 2

ws.test.tsFile · 0.90
connectMethod · 0.90

Calls 1

startPingingFunction · 0.85

Tested by

no test coverage detected