dev #14

Merged
bvbej merged 2 commits from dev into main 2024-09-07 15:55:29 +08:00

View File

@ -1,9 +1,7 @@
package sse
import (
"fmt"
"gitea.bvbej.com/bvbej/base-golang/pkg/mux"
"gitea.bvbej.com/bvbej/base-golang/pkg/ticker"
"github.com/gin-gonic/gin"
"io"
"net/http"
@ -36,7 +34,6 @@ type event struct {
Count atomic.Int32
Register chan clientChan
Unregister chan any
Ticker ticker.Ticker
}
func NewServer() Server {
@ -45,12 +42,8 @@ func NewServer() Server {
Count: atomic.Int32{},
Register: make(chan clientChan),
Unregister: make(chan any),
Ticker: ticker.New(time.Second * 3),
}
go e.Ticker.Process(func() {
e.Broadcast("ping", fmt.Sprintf("%d", time.Now().Unix()))
})
go e.listen()
return e
@ -130,6 +123,10 @@ func (stream *event) GinHandlerFunc(auth func(c *gin.Context) (string, error)) g
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Transfer-Encoding", "chunked")
time.AfterFunc(time.Second, func() {
e <- msgChan{Name: "message", Message: "success"}
})
c.Stream(func(w io.Writer) bool {
if msg, ok := <-e; ok {
c.SSEvent(msg.Name, msg.Message)