live.go 766 B

123456789101112131415161718192021222324252627282930313233
  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. // https://stackoverflow.com/questions/27898622/server-sent-events-stopped-work-after-enabling-ssl-on-proxy/27960243#27960243
  13. c.Header("X-Accel-Buffering", "no")
  14. evtChan := make(chan *model.Notification)
  15. notification.SetClient(c, evtChan)
  16. notify := c.Writer.CloseNotify()
  17. go func() {
  18. <-notify
  19. notification.RemoveClient(c)
  20. }()
  21. for n := range evtChan {
  22. c.Stream(func(w io.Writer) bool {
  23. c.SSEvent("message", n)
  24. return false
  25. })
  26. }
  27. }