dev #14
@ -1,9 +1,7 @@
|
|||||||
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"
|
||||||
@ -36,7 +34,6 @@ 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 {
|
||||||
@ -45,12 +42,8 @@ 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
|
||||||
@ -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("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…
Reference in New Issue
Block a user