Skip to content

Commit dc802b0

Browse files
nugaonbosi95
authored andcommitted
fix: pingpong
1 parent 153215b commit dc802b0

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

src/bee.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,10 +1320,33 @@ export class Bee {
13201320
this.requestOptions.headers,
13211321
)
13221322

1323+
const PING_INTERVAL_MS = 50_000
1324+
let pingTimer: ReturnType<typeof setInterval> | null = null
1325+
1326+
const startPing = () => {
1327+
if (typeof ws.ping === 'function') {
1328+
pingTimer = setInterval(() => {
1329+
try {
1330+
ws.ping()
1331+
} catch {
1332+
// ignore errors on closed sockets
1333+
}
1334+
}, PING_INTERVAL_MS)
1335+
}
1336+
}
1337+
1338+
const stopPing = () => {
1339+
if (pingTimer !== null) {
1340+
clearInterval(pingTimer)
1341+
pingTimer = null
1342+
}
1343+
}
1344+
13231345
let cancelled = false
13241346
const cancel = () => {
13251347
if (!cancelled) {
13261348
cancelled = true
1349+
stopPing()
13271350

13281351
if (ws.terminate) {
13291352
ws.terminate()
@@ -1359,6 +1382,7 @@ export class Bee {
13591382
}
13601383

13611384
ws.onopen = () => {
1385+
startPing()
13621386
flushQueue()
13631387
}
13641388

@@ -1375,6 +1399,8 @@ export class Bee {
13751399
}
13761400
}
13771401
ws.onclose = () => {
1402+
stopPing()
1403+
13781404
if (!cancelled) {
13791405
handler.onClose(subscription)
13801406
}

0 commit comments

Comments
 (0)