sse

package
v0.94.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package sse SSE 的实现

Index

Constants

View Source
const Mimetype = header.EventStream

Mimetype sse 请求从服务端返回给客户端的内容类型

Variables

This section is empty.

Functions

func OnMessage

func OnMessage(ctx context.Context, l *web.Logger, url string, c *http.Client, msg chan *Message) error

OnMessage 对消息的处理

l 用于记录运行过程的错误信息; msg 用于接收从服务端返回的数据对象。 从 msg 中取出的 Message 对象,在不再需要时可以调用 Message.Free 回收;

NOTE: 提交的请求中会将 Accept 报头设置为 Mimetype,服务端需要能正确处理该值才行。

Types

type MarshalFunc

type MarshalFunc = func(any) ([]byte, error)

type Message

type Message struct {
	Data  []string
	Event string
	ID    string
	Retry int64
}

func (*Message) Free

func (m *Message) Free()

Free 销毁当前对象

NOTE: 这不是一个必须的操作,在确定不再使用当前对象的情况下, 执行该方法,有可能会提升一些性能。

type Server

type Server[T comparable] struct {
	// contains filtered or unexported fields
}

Server SSE 服务端

T 表示用于区分不同事件源的 ID,比如按用户区分, 那么该类型可能是 int64 类型的用户 ID 值。

func NewServer

func NewServer[T comparable](s web.Server, retry, keepAlive time.Duration, bufCap int) *Server[T]

NewServer 声明 Server 对象

retry 表示反馈给用户的 retry 字段,可以为零值,表示不需要输出该字段; keepAlive 表示心跳包的发送时间间隔,如果小于等于零,表示不会发送; bufCap 每个 SSE 队列可缓存的数据,超过此数量,调用的 Sent 将被阻塞;

func (*Server[T]) Get

func (srv *Server[T]) Get(sid T) *Source

Get 返回指定 sid 的事件源

仅在 Server.NewSource 执行之后,此函数才能返回非空值。

func (*Server[T]) Len

func (srv *Server[T]) Len() (size int)

Len 当前链接的数量

func (*Server[T]) NewEvent

func (srv *Server[T]) NewEvent(name string, marshal MarshalFunc) *ServerEvent[T]

NewEvent 声明具有统一编码方式的事件派发对象

name 表示事件名称,最终输出为 event 字段; marshal 表示 data 字段的编码方式;

func (*Server[T]) NewSource

func (srv *Server[T]) NewSource(sid T, ctx *web.Context) (s *Source, wait func())

NewSource 声明新的事件源

NOTE: 只有采用此方法声明之后,才有可能通过 Server.Get 获取实例。 sid 表示是事件源的唯一 ID,如果事件是根据用户进行区分的,那么该值应该是表示用户的 ID 值; wait 当前 s 退出时,wait 才会返回,可以在 web.Handler 中阻止路由退出导致的 ctx 被回收。

func (*Server[T]) Range

func (srv *Server[T]) Range(f func(sid T, s *Source))

Range 依次访问注册的 Source 对象

type ServerEvent

type ServerEvent[T comparable] struct {
	// contains filtered or unexported fields
}

func (*ServerEvent[T]) Sent

func (e *ServerEvent[T]) Sent(f func(sid T, lastEventID string) any)

Sent 向所有注册的 Source 发送由 f 生成的对象

type Source

type Source struct {
	// contains filtered or unexported fields
}

func (*Source) Close

func (s *Source) Close()

Close 关闭当前事件源

这将导致关联的 Server.NewSource 的 wait 直接返回。

func (*Source) LastEventID

func (s *Source) LastEventID() string

LastEventID 客户端提交的报头 Last-Event-ID 值

当此值不为空时表示该链接刚从客户端重新连接上。 有新内容发送给客户端之后,该值会被重置为空。

func (*Source) NewEvent

func (s *Source) NewEvent(name string, marshal MarshalFunc) *SourceEvent

NewEvent 声明具有统一编码方式的事件派发对象

name 表示事件名称,最终输出为 event 字段; marshal 表示 data 字段的编码方式;

func (*Source) Sent

func (s *Source) Sent(data []string, event, id string)

Sent 发送消息

id 和 event 都可以为空,表示不需要这些值; 如果不想输出 retry 可以输出一个非整数,按照规则客户端会忽略非整数的值;

type SourceEvent

type SourceEvent struct {
	// contains filtered or unexported fields
}

func (*SourceEvent) Sent

func (e *SourceEvent) Sent(obj any) error

func (*SourceEvent) Source

func (e *SourceEvent) Source() *Source

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL