Compare commits
No commits in common. "a0b561237aa2722c4a27217eae4a4a8248962353" and "d3513ae4fad57176cc417ec0570270c5f9e14c29" have entirely different histories.
a0b561237a
...
d3513ae4fa
@ -1,7 +1,9 @@
|
|||||||
package sse
|
package sse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"gitea.bvbej.com/bvbej/base-golang/pkg/mux"
|
"gitea.bvbej.com/bvbej/base-golang/pkg/mux"
|
||||||
|
"gitea.bvbej.com/bvbej/base-golang/pkg/ticker"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -34,6 +36,7 @@ type event struct {
|
|||||||
Count atomic.Int32
|
Count atomic.Int32
|
||||||
Register chan clientChan
|
Register chan clientChan
|
||||||
Unregister chan any
|
Unregister chan any
|
||||||
|
Ticker ticker.Ticker
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() Server {
|
func NewServer() Server {
|
||||||
@ -42,8 +45,12 @@ func NewServer() Server {
|
|||||||
Count: atomic.Int32{},
|
Count: atomic.Int32{},
|
||||||
Register: make(chan clientChan),
|
Register: make(chan clientChan),
|
||||||
Unregister: make(chan any),
|
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()
|
go e.listen()
|
||||||
|
|
||||||
return e
|
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("Connection", "keep-alive")
|
||||||
c.Writer.Header().Set("Transfer-Encoding", "chunked")
|
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 {
|
c.Stream(func(w io.Writer) bool {
|
||||||
if msg, ok := <-e; ok {
|
if msg, ok := <-e; ok {
|
||||||
c.SSEvent(msg.Name, msg.Message)
|
c.SSEvent(msg.Name, msg.Message)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user