Compare commits
No commits in common. "2da7c8a4c1eceacdc086cea2a996b427696fa679" and "b5406b1451e62c90e7fe3b85b3ff61019605526b" have entirely different histories.
2da7c8a4c1
...
b5406b1451
@ -1,7 +1,6 @@
|
|||||||
package sse
|
package sse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gitea.bvbej.com/bvbej/base-golang/pkg/mux"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -12,14 +11,13 @@ import (
|
|||||||
var _ Server = (*event)(nil)
|
var _ Server = (*event)(nil)
|
||||||
|
|
||||||
type Server interface {
|
type Server interface {
|
||||||
GinHandle(ctx *gin.Context, user any)
|
HandlerFunc(auth func(c *gin.Context) (string, error)) gin.HandlerFunc
|
||||||
HandlerFunc() mux.HandlerFunc
|
|
||||||
Push(user, name, msg string) bool
|
Push(user, name, msg string) bool
|
||||||
Broadcast(name, msg string)
|
Broadcast(name, msg string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientChan struct {
|
type clientChan struct {
|
||||||
User any
|
User string
|
||||||
Chan chan msgChan
|
Chan chan msgChan
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +31,7 @@ type event struct {
|
|||||||
Count atomic.Int32
|
Count atomic.Int32
|
||||||
|
|
||||||
Register chan clientChan
|
Register chan clientChan
|
||||||
Unregister chan any
|
Unregister chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() Server {
|
func NewServer() Server {
|
||||||
@ -41,7 +39,7 @@ func NewServer() Server {
|
|||||||
SessionList: sync.Map{},
|
SessionList: sync.Map{},
|
||||||
Count: atomic.Int32{},
|
Count: atomic.Int32{},
|
||||||
Register: make(chan clientChan),
|
Register: make(chan clientChan),
|
||||||
Unregister: make(chan any),
|
Unregister: make(chan string),
|
||||||
}
|
}
|
||||||
|
|
||||||
go e.listen()
|
go e.listen()
|
||||||
@ -67,11 +65,14 @@ func (stream *event) listen() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *event) GinHandle(ctx *gin.Context, user any) {
|
func (stream *event) HandlerFunc(auth func(c *gin.Context) (string, error)) gin.HandlerFunc {
|
||||||
if user == nil {
|
return func(c *gin.Context) {
|
||||||
ctx.AbortWithStatus(http.StatusUnauthorized)
|
user, err := auth(c)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e := make(chan msgChan)
|
e := make(chan msgChan)
|
||||||
client := clientChan{
|
client := clientChan{
|
||||||
User: user,
|
User: user,
|
||||||
@ -82,25 +83,20 @@ func (stream *event) GinHandle(ctx *gin.Context, user any) {
|
|||||||
stream.Unregister <- user
|
stream.Unregister <- user
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ctx.Writer.Header().Set("Content-Type", "text/event-stream")
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||||
ctx.Writer.Header().Set("Cache-Control", "no-cache")
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
||||||
ctx.Writer.Header().Set("Connection", "keep-alive")
|
c.Writer.Header().Set("Connection", "keep-alive")
|
||||||
ctx.Writer.Header().Set("Transfer-Encoding", "chunked")
|
c.Writer.Header().Set("Transfer-Encoding", "chunked")
|
||||||
|
|
||||||
ctx.Stream(func(w io.Writer) bool {
|
c.Stream(func(w io.Writer) bool {
|
||||||
if msg, ok := <-e; ok {
|
if msg, ok := <-e; ok {
|
||||||
ctx.SSEvent(msg.Name, msg.Message)
|
c.SSEvent(msg.Name, msg.Message)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.Next()
|
c.Next()
|
||||||
}
|
|
||||||
|
|
||||||
func (stream *event) HandlerFunc() mux.HandlerFunc {
|
|
||||||
return func(c mux.Context) {
|
|
||||||
stream.GinHandle(c.Context(), c.Auth())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user