mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
feat: sse notify
This commit is contained in:
parent
b4add42039
commit
e6e1876c54
11 changed files with 187 additions and 47 deletions
31
api/notification/live.go
Normal file
31
api/notification/live.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package notification
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/notification"
|
||||
"github.com/0xJacky/Nginx-UI/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
)
|
||||
|
||||
func Live(c *gin.Context) {
|
||||
c.Header("Content-Type", "text/event-stream")
|
||||
c.Header("Cache-Control", "no-cache")
|
||||
c.Header("Connection", "keep-alive")
|
||||
|
||||
evtChan := make(chan *model.Notification)
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
|
@ -7,4 +7,6 @@ func InitRouter(r *gin.RouterGroup) {
|
|||
r.GET("notifications/:id", Get)
|
||||
r.DELETE("notifications/:id", Destroy)
|
||||
r.DELETE("notifications", DestroyAll)
|
||||
|
||||
r.GET("notifications/live", Live)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue