live.go 602 B

12345678910111213141516171819202122232425262728293031
  1. package notification
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/notification"
  4. "github.com/0xJacky/Nginx-UI/model"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. )
  8. func Live(c *gin.Context) {
  9. c.Header("Content-Type", "text/event-stream")
  10. c.Header("Cache-Control", "no-cache")
  11. c.Header("Connection", "keep-alive")
  12. evtChan := make(chan *model.Notification)
  13. notification.SetClient(c, evtChan)
  14. notify := c.Writer.CloseNotify()
  15. go func() {
  16. <-notify
  17. notification.RemoveClient(c)
  18. }()
  19. for n := range evtChan {
  20. c.Stream(func(w io.Writer) bool {
  21. c.SSEvent("message", n)
  22. return false
  23. })
  24. }
  25. }