sse

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sse 提供 Server-Sent Events 实时推送能力: 客户端通过 HTTP 长连接订阅事件,服务端按事件名定向或广播推送。 配合 eventbus 可把进程内事件桥接到浏览器端。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client 表示一个 SSE 长连接客户端。

func (*Client) ID

func (c *Client) ID() string

ID 返回客户端唯一标识。

func (*Client) IsClosed

func (c *Client) IsClosed() bool

IsClosed 判断连接是否已关闭。

func (*Client) IsSubscribed

func (c *Client) IsSubscribed(event string) bool

IsSubscribed 判断是否订阅了指定事件。

func (*Client) Subscribe

func (c *Client) Subscribe(events ...string)

Subscribe 追加订阅事件。

type Manager

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

Manager 管理全部 SSE 客户端。

func NewManager

func NewManager() *Manager

NewManager 创建 SSE 客户端管理器。

func (*Manager) BridgeEventBus

func (m *Manager) BridgeEventBus(ctx context.Context, bus *eventbus.Bus) func()

BridgeEventBus 把 eventbus 事件桥接到 SSE 广播(事件名作为 SSE event 名)。 返回取消函数;ctx 取消或调用取消函数时自动解除桥接。

func (*Manager) Broadcast

func (m *Manager) Broadcast(event string, data interface{}) error

Broadcast 广播事件到所有订阅了该事件的客户端。

func (*Manager) Close

func (m *Manager) Close() error

Close 关闭全部客户端连接。

func (*Manager) Count

func (m *Manager) Count() int

Count 返回当前在线客户端数。

func (*Manager) Handle

func (m *Manager) Handle(ctx iris.Context)

Handle 是 Iris SSE 处理器:升级连接、按 events 订阅、循环推送直到连接断开。 用法:app.Get("/events", sseManager.Handle, ...) —— 注意事件参数无法通过路由传递, 如需指定订阅事件,使用 HandleEvents(ctx, events...)。

func (*Manager) HandleEvents

func (m *Manager) HandleEvents(ctx iris.Context, events ...string)

HandleEvents 是带订阅事件列表的 Iris SSE 处理器。

func (*Manager) Handler

func (m *Manager) Handler(events ...string) http.HandlerFunc

Handler 返回标准库 SSE 处理器;可配合 iris.FromStd 使用,或直接用于 net/http。 events 为该连接订阅的事件;连接断开时自动注销。

func (*Manager) SendTo

func (m *Manager) SendTo(clientID, event string, data interface{}) error

SendTo 定向推送事件到指定客户端;客户端未订阅该事件时忽略。

func (*Manager) WithHeartbeat

func (m *Manager) WithHeartbeat(interval time.Duration) *Manager

WithHeartbeat 设置心跳间隔(非正数使用默认值)。

Jump to

Keyboard shortcuts

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