diff --git a/internal/cmd/ocean/spark_connect.go b/internal/cmd/ocean/spark_connect.go index f8fc1657..16aba6f2 100644 --- a/internal/cmd/ocean/spark_connect.go +++ b/internal/cmd/ocean/spark_connect.go @@ -274,6 +274,9 @@ func handleSocketConnection(ctx context.Context, conn net.Conn, wsConn *websocke return fromUpstream(conn, wsConn) }) + // Send websocket ping periodically to keep connection alive + g.Go(func() error { return sendPing(ctx, wsConn) }) + return g.Wait() } @@ -315,3 +318,19 @@ func toUpstream(downstream io.Writer, upstream *websocket.Conn) error { return nil } + +func sendPing(ctx context.Context, wsConn *websocket.Conn) error { + ticker := time.NewTicker(5 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return nil + case <-ticker.C: + if err := wsConn.WriteMessage(websocket.PingMessage, nil); err != nil { + return err + } + } + } +}