File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments