commit
2da7c8a4c1
@ -1,6 +1,7 @@
|
|||||||
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"
|
||||||
@ -11,13 +12,14 @@ import (
|
|||||||
var _ Server = (*event)(nil)
|
var _ Server = (*event)(nil)
|
||||||
|
|
||||||
type Server interface {
|
type Server interface {
|
||||||
HandlerFunc(auth func(c *gin.Context) (string, error)) gin.HandlerFunc
|
GinHandle(ctx *gin.Context, user any)
|
||||||
|
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 string
|
User any
|
||||||
Chan chan msgChan
|
Chan chan msgChan
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +33,7 @@ type event struct {
|
|||||||
Count atomic.Int32
|
Count atomic.Int32
|
||||||
|
|
||||||
Register chan clientChan
|
Register chan clientChan
|
||||||
Unregister chan string
|
Unregister chan any
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() Server {
|
func NewServer() Server {
|
||||||
@ -39,7 +41,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 string),
|
Unregister: make(chan any),
|
||||||
}
|
}
|
||||||
|
|
||||||
go e.listen()
|
go e.listen()
|
||||||
@ -65,38 +67,40 @@ func (stream *event) listen() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *event) HandlerFunc(auth func(c *gin.Context) (string, error)) gin.HandlerFunc {
|
func (stream *event) GinHandle(ctx *gin.Context, user any) {
|
||||||
return func(c *gin.Context) {
|
if user == nil {
|
||||||
user, err := auth(c)
|
ctx.AbortWithStatus(http.StatusUnauthorized)
|
||||||
if err != nil {
|
return
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
}
|
||||||
return
|
e := make(chan msgChan)
|
||||||
|
client := clientChan{
|
||||||
|
User: user,
|
||||||
|
Chan: e,
|
||||||
|
}
|
||||||
|
stream.Register <- client
|
||||||
|
defer func() {
|
||||||
|
stream.Unregister <- user
|
||||||
|
}()
|
||||||
|
|
||||||
|
ctx.Writer.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
ctx.Writer.Header().Set("Cache-Control", "no-cache")
|
||||||
|
ctx.Writer.Header().Set("Connection", "keep-alive")
|
||||||
|
ctx.Writer.Header().Set("Transfer-Encoding", "chunked")
|
||||||
|
|
||||||
|
ctx.Stream(func(w io.Writer) bool {
|
||||||
|
if msg, ok := <-e; ok {
|
||||||
|
ctx.SSEvent(msg.Name, msg.Message)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
e := make(chan msgChan)
|
ctx.Next()
|
||||||
client := clientChan{
|
}
|
||||||
User: user,
|
|
||||||
Chan: e,
|
|
||||||
}
|
|
||||||
stream.Register <- client
|
|
||||||
defer func() {
|
|
||||||
stream.Unregister <- user
|
|
||||||
}()
|
|
||||||
|
|
||||||
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
func (stream *event) HandlerFunc() mux.HandlerFunc {
|
||||||
c.Writer.Header().Set("Cache-Control", "no-cache")
|
return func(c mux.Context) {
|
||||||
c.Writer.Header().Set("Connection", "keep-alive")
|
stream.GinHandle(c.Context(), c.Auth())
|
||||||
c.Writer.Header().Set("Transfer-Encoding", "chunked")
|
|
||||||
|
|
||||||
c.Stream(func(w io.Writer) bool {
|
|
||||||
if msg, ok := <-e; ok {
|
|
||||||
c.SSEvent(msg.Name, msg.Message)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
c.Next()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user