Skip to content

Commit

Permalink
enhance: add heartbeat for sse #705
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Nov 8, 2024
1 parent 5698418 commit 21d927e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
26 changes: 21 additions & 5 deletions api/notification/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"io"
"time"
)

func Live(c *gin.Context) {
Expand All @@ -24,10 +25,25 @@ func Live(c *gin.Context) {
notification.RemoveClient(c)
}()

for n := range evtChan {
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", n)
return false
})
c.Stream(func(w io.Writer) bool {
c.SSEvent("heartbeat", "")
return false
})

timeout := time.NewTicker(30 * time.Second)

for {
select {
case n := <-evtChan:
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", n)
return false
})
case <-timeout.C:
c.Stream(func(w io.Writer) bool {
c.SSEvent("heartbeat", "")
return false
})
}
}
}
1 change: 0 additions & 1 deletion app/src/components/Notification/Notification.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function newSSE() {
// reconnect
s.onerror = reconnect
s.onabort = reconnect
return s
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"2.0.0-beta.39","build_id":1,"total_build":368}
{"version":"2.0.0-beta.39","build_id":3,"total_build":370}

0 comments on commit 21d927e

Please sign in to comment.