Documentation
¶
Overview ¶
Package sse SSE 的实现
Index ¶
- Constants
- func OnMessage(ctx context.Context, l web.Logger, req *http.Request, c *http.Client, ...) error
- type Message
- type Server
- func (srv *Server[T]) Get(sid T) *Source
- func (srv *Server[T]) Len() (size int)
- func (srv *Server[T]) NewEvent(name string, marshal web.MarshalFunc) *ServerEvent[T]
- func (srv *Server[T]) NewSource(sid T, ctx *web.Context) (s *Source, wait func())
- func (srv *Server[T]) Range(f func(sid T, s *Source))
- type ServerEvent
- type Source
- type SourceEvent
Constants ¶
const Mimetype = "text/event-stream"
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Server ¶ added in v0.85.0
type Server[T comparable] struct { // contains filtered or unexported fields }
Server SSE 服务端
T 表示用于区分不同事件源的 ID,比如按用户区分, 那么该类型可能是 int64 类型的用户 ID 值。
func NewServer ¶ added in v0.85.0
NewServer 声明 Server 对象
retry 表示反馈给用户的 retry 字段,可以为零值,表示不需要输出该字段; keepAlive 表示心跳包的发送时间间隔,如果小于等于零,表示不会发送; bufCap 每个 SSE 队列可缓存的数据,超过此数量,调用的 Sent 将被阻塞;
func (*Server[T]) Get ¶ added in v0.85.0
func (srv *Server[T]) Get(sid T) *Source
Get 返回指定 sid 的事件源
仅在 Server.NewSource 执行之后,此函数才能返回非空值。
func (*Server[T]) NewEvent ¶ added in v0.85.0
func (srv *Server[T]) NewEvent(name string, marshal web.MarshalFunc) *ServerEvent[T]
NewEvent 声明具有统一编码方式的事件派发对象
name 表示事件名称,最终输出为 event 字段; marshal 表示 data 字段的编码方式;
func (*Server[T]) NewSource ¶ added in v0.85.0
NewSource 声明新的事件源
NOTE: 只有采用此方法声明之后,才有可能通过 Server.Get 获取实例。 sid 表示是事件源的唯一 ID,如果事件是根据用户进行区分的,那么该值应该是表示用户的 ID 值; wait 当前 s 退出时,wait 才会返回,可以在 web.Handler 中阻止路由退出导致的 ctx 被回收。
type ServerEvent ¶ added in v0.85.0
type ServerEvent[T comparable] struct { // contains filtered or unexported fields }
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
func (*Source) LastEventID ¶ added in v0.85.0
func (s *Source) LastEventID() string
LastEventID 客户端提交的报头 Last-Event-ID 值
当此值不为空时表示该链接刚从客户端重新连接上。 有新内容发送给客户端之后,该值会被重置为空。
func (*Source) NewEvent ¶ added in v0.85.0
func (s *Source) NewEvent(name string, marshal web.MarshalFunc) *SourceEvent
NewEvent 声明具有统一编码方式的事件派发对象
name 表示事件名称,最终输出为 event 字段; marshal 表示 data 字段的编码方式;