|
@@ -5,6 +5,7 @@ import (
|
|
|
"github.com/0xJacky/Nginx-UI/model"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"io"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
func Live(c *gin.Context) {
|
|
@@ -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
|
|
|
+ }
|
|
|
}
|
|
|
}
|