From 61099ee7344f4ba498a1f4888038dd679de4ab7d Mon Sep 17 00:00:00 2001 From: louisheal Date: Wed, 8 Jan 2025 14:34:44 +0000 Subject: [PATCH] send heartbeat every 60s --- controllers/overlay.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/controllers/overlay.go b/controllers/overlay.go index bb48077..73048ce 100644 --- a/controllers/overlay.go +++ b/controllers/overlay.go @@ -2,6 +2,7 @@ package controllers import ( "io" + "time" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -68,11 +69,20 @@ func (c *OverlayController) HandleListen(ctx *gin.Context) { c.Clients.RemoveClient(client) }() + ticker := time.NewTicker(60 * time.Second) + defer ticker.Stop() + ctx.Stream(func(w io.Writer) bool { - if event, ok := <-client.Stream; ok { - ctx.SSEvent(event.Event, event.Message) + select { + case event, ok := <-client.Stream: + if ok { + ctx.SSEvent(event.Event, event.Message) + return true + } + return false + case <-ticker.C: + ctx.SSEvent("heartbeat", "ping") return true } - return false }) }