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 1466987
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
31 changes: 22 additions & 9 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 @@ -19,15 +20,27 @@ func Live(c *gin.Context) {
notification.SetClient(c, evtChan)

notify := c.Writer.CloseNotify()
go func() {
<-notify
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
})

for {
select {
case n := <-evtChan:
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", n)
return false
})
case <-time.After(30 * time.Second):
c.Stream(func(w io.Writer) bool {
c.SSEvent("heartbeat", "")
return false
})
case <-notify:
notification.RemoveClient(c)
return
}
}
}
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}
3 changes: 2 additions & 1 deletion internal/nginx/ngx_conf_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func TestNgxConfParse(t *testing.T) {
fmt.Println(err)
return
}
n := p.Parse()

n, _ := p.Parse()

fn(n.Block, 0)

Expand Down

0 comments on commit 1466987

Please sign in to comment.