Compare commits
3 Commits
d3513ae4fa
...
a0b561237a
Author | SHA1 | Date | |
---|---|---|---|
a0b561237a | |||
ad1e06229f | |||
e99a8dbcd6 |
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user