Browse Source

enhance: add heartbeat for sse #705

Jacky 11 months ago
parent
commit
21d927e86b
3 changed files with 22 additions and 7 deletions
  1. 21 5
      api/notification/live.go
  2. 0 1
      app/src/components/Notification/Notification.vue
  3. 1 1
      app/src/version.json

+ 21 - 5
api/notification/live.go

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

+ 0 - 1
app/src/components/Notification/Notification.vue

@@ -58,7 +58,6 @@ function newSSE() {
 
   // reconnect
   s.onerror = reconnect
-  s.onabort = reconnect
 
   return s
 }

+ 1 - 1
app/src/version.json

@@ -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}