sse

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package sse 提供基于Server-Sent Events的实时通信功能

Index

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 在注册前应用客户端配置

func PutEvent

func PutEvent(event *Event)

PutEvent 将事件对象放回对象池

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 客户端结构体

func NewClient

func NewClient(w http.ResponseWriter, r *http.Request, filters ...string) *Client

NewClient 创建新的 SSE 客户端连接

func (*Client) Close

func (c *Client) Close()

Close 关闭客户端连接

func (*Client) Listen

func (c *Client) Listen()

Listen 监听并处理客户端消息

type ClientOption

type ClientOption func(*Client)

ClientOption 自定义客户端属性

func WithClientID

func WithClientID(id string) ClientOption

WithClientID 指定客户端 ID

type Config

type Config struct {
	// HistorySize 历史记录大小
	HistorySize int
	// PingTimeout 心跳超时时间
	PingTimeout time.Duration
	// PingInterval 心跳间隔
	PingInterval time.Duration
	// RetryInterval 重试间隔(毫秒)
	RetryInterval int
}

Config Hub配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

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 事件结构体

func GetEvent

func GetEvent() *Event

GetEvent 从对象池获取事件对象

type Hub

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

Hub SSE 连接管理中心

func NewHub

func NewHub(config *Config) *Hub

NewHub 创建新的 SSE Hub

func (*Hub) Broadcast

func (h *Hub) Broadcast(event *Event)

Broadcast 广播消息

func (*Hub) Close

func (h *Hub) Close()

Close 关闭 Hub

func (*Hub) GetClients

func (h *Hub) GetClients() []string

GetClients 获取所有在线客户端ID

func (*Hub) GetPerformanceMetrics

func (h *Hub) GetPerformanceMetrics() map[string]interface{}

GetPerformanceMetrics 获取性能指标

func (*Hub) GetStats

func (h *Hub) GetStats() map[string]interface{}

GetStats 获取Hub性能统计信息

func (*Hub) IsRunning

func (h *Hub) IsRunning() bool

IsRunning 检查 Hub 是否正在运行

func (*Hub) RegisterClient

func (h *Hub) RegisterClient(client *Client)

RegisterClient 注册客户端到Hub

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run 运行 SSE Hub

func (*Hub) SendToClient

func (h *Hub) SendToClient(clientID string, event *Event) bool

SendToClient 发送消息到指定客户端

func (*Hub) UnregisterClient

func (h *Hub) UnregisterClient(client *Client)

UnregisterClient 从Hub注销客户端

Jump to

Keyboard shortcuts

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