Documentation
¶
Overview ¶
Package sse 提供基于Server-Sent Events的实时通信功能
Index ¶
- Constants
- func ApplyClientOptions(client *Client, opts ...ClientOption)
- func PutEvent(event *Event)
- type Client
- type ClientOption
- type Config
- type Event
- type Hub
- func (h *Hub) Broadcast(event *Event)
- func (h *Hub) Close()
- func (h *Hub) GetClients() []string
- func (h *Hub) GetPerformanceMetrics() map[string]interface{}
- func (h *Hub) GetStats() map[string]interface{}
- func (h *Hub) IsRunning() bool
- func (h *Hub) RegisterClient(client *Client)
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) SendToClient(clientID string, event *Event) bool
- func (h *Hub) UnregisterClient(client *Client)
Constants ¶
View Source
const ( // DefaultHistorySize 默认历史记录大小 DefaultHistorySize = 50 // DefaultBufferSize 默认缓冲区大小 DefaultBufferSize = 512 // DefaultRetryInterval 默认重试间隔(毫秒) DefaultRetryInterval = 3000 // DefaultPingInterval 默认心跳间隔 DefaultPingInterval = 10 * time.Second // DefaultPingTimeout 默认心跳超时时间 DefaultPingTimeout = 30 * time.Second )
Variables ¶
This section is empty.
Functions ¶
func ApplyClientOptions ¶
func ApplyClientOptions(client *Client, opts ...ClientOption)
ApplyClientOptions 在注册前应用客户端配置
Types ¶
type Client ¶
type Client struct {
// ID 客户端唯一标识
ID string
// ResponseWriter 客户端连接的响应写入器
ResponseWriter http.ResponseWriter
// Request 客户端请求
Request *http.Request
// MessageChan 消息通道
MessageChan chan *Event
// Disconnected 断开连接信号
Disconnected chan struct{}
// CloseOnce 确保只关闭一次
CloseOnce sync.Once
// LastEventID 最后接收的事件ID
LastEventID string
// EventFilters 事件过滤器,只接收指定事件
EventFilters []string
// contains filtered or unexported fields
}
Client SSE 客户端结构体
type Config ¶
type Config struct {
// HistorySize 历史记录大小
HistorySize int
// PingTimeout 心跳超时时间
PingTimeout time.Duration
// PingInterval 心跳间隔
PingInterval time.Duration
// RetryInterval 重试间隔(毫秒)
RetryInterval int
}
Config Hub配置
type Event ¶
type Event struct {
// Event 事件名称
Event string `json:"event"`
// Data 事件数据
Data any `json:"data"`
// ID 事件ID
ID string `json:"id"`
// Retry 重试时间(毫秒)
Retry int `json:"retry"`
}
Event SSE 事件结构体
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub SSE 连接管理中心
func (*Hub) GetPerformanceMetrics ¶
GetPerformanceMetrics 获取性能指标
func (*Hub) SendToClient ¶
SendToClient 发送消息到指定客户端
func (*Hub) UnregisterClient ¶
UnregisterClient 从Hub注销客户端
Click to show internal directories.
Click to hide internal directories.