Skip to content

Commit

Permalink
send heartbeat every 60s
Browse files Browse the repository at this point in the history
  • Loading branch information
louisheal committed Jan 8, 2025
1 parent 155b33c commit 61099ee
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions controllers/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"io"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
Expand Down Expand Up @@ -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
})
}

0 comments on commit 61099ee

Please sign in to comment.