Compare commits
No commits in common. "a0b561237aa2722c4a27217eae4a4a8248962353" and "d3513ae4fad57176cc417ec0570270c5f9e14c29" have entirely different histories.
a0b561237a
...
d3513ae4fa
@ -1,7 +1,9 @@
|
||||
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"
|
||||
@ -34,6 +36,7 @@ type event struct {
|
||||
Count atomic.Int32
|
||||
Register chan clientChan
|
||||
Unregister chan any
|
||||
Ticker ticker.Ticker
|
||||
}
|
||||
|
||||
func NewServer() Server {
|
||||
@ -42,8 +45,12 @@ 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
|
||||
@ -123,10 +130,6 @@ 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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user