Compare commits

..

No commits in common. "main" and "v0.12.12" have entirely different histories.

View File

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
) )
var _ Server = (*event)(nil) var _ Server = (*event)(nil)
@ -17,7 +16,6 @@ type Server interface {
GinHandlerFunc(auth func(c *gin.Context) (string, error)) gin.HandlerFunc GinHandlerFunc(auth func(c *gin.Context) (string, error)) gin.HandlerFunc
Push(user any, name, msg string) bool Push(user any, name, msg string) bool
Broadcast(name, msg string) Broadcast(name, msg string)
ClientCount() int32
} }
type clientChan struct { type clientChan struct {
@ -33,6 +31,7 @@ type msgChan struct {
type event struct { type event struct {
SessionList sync.Map SessionList sync.Map
Count atomic.Int32 Count atomic.Int32
Register chan clientChan Register chan clientChan
Unregister chan any Unregister chan any
} }
@ -91,10 +90,6 @@ func (stream *event) HandlerFunc() mux.HandlerFunc {
c.Context().Writer.Header().Set("Connection", "keep-alive") c.Context().Writer.Header().Set("Connection", "keep-alive")
c.Context().Writer.Header().Set("Transfer-Encoding", "chunked") c.Context().Writer.Header().Set("Transfer-Encoding", "chunked")
time.AfterFunc(time.Second, func() {
e <- msgChan{Name: "message", Message: "success"}
})
c.Context().Stream(func(w io.Writer) bool { c.Context().Stream(func(w io.Writer) bool {
if msg, ok := <-e; ok { if msg, ok := <-e; ok {
c.Context().SSEvent(msg.Name, msg.Message) c.Context().SSEvent(msg.Name, msg.Message)
@ -128,10 +123,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)
@ -158,7 +149,3 @@ func (stream *event) Broadcast(name, msg string) {
return true return true
}) })
} }
func (stream *event) ClientCount() int32 {
return stream.Count.Load()
}