realtime

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hub

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

func NewHub

func NewHub(cfg *config.Config) *Hub

func (*Hub) Broadcast added in v0.2.4

func (h *Hub) Broadcast(data any)

Broadcast 把同一条消息推给所有在线连接(跨用户),用于与用户无关的全局事件 (例如「容器队列发生了变化」这种所有在线客户端都该刷新的通知)。

与 PushMessage 不同,没有在线连接不算错误:广播本身就是尽力而为。

func (*Hub) HandleSSE

func (h *Hub) HandleSSE(w http.ResponseWriter, r *http.Request, userID int64) error

func (*Hub) HandleWebSocket

func (h *Hub) HandleWebSocket(w http.ResponseWriter, r *http.Request, userID int64) error

func (*Hub) MaxConnectionsPerUser

func (h *Hub) MaxConnectionsPerUser() int

func (*Hub) PushMessage

func (h *Hub) PushMessage(userID int64, data any) error

func (*Hub) PushMessageWaitAck

func (h *Hub) PushMessageWaitAck(userID int64, data any, timeout time.Duration) PushAckResult

func (*Hub) Stats

func (h *Hub) Stats() Stats

func (*Hub) SubscribeInbound

func (h *Hub) SubscribeInbound(handler func(InboundMessage)) func()

func (*Hub) Transport

func (h *Hub) Transport() Transport

type InboundMessage

type InboundMessage struct {
	UserID     int64           `json:"user_id,string"`
	ClientID   string          `json:"client_id"`
	Transport  Transport       `json:"transport"`
	Payload    map[string]any  `json:"payload"`
	Raw        json.RawMessage `json:"raw"`
	ReceivedAt time.Time       `json:"received_at"`
}

type PushAckResult

type PushAckResult struct {
	OK        bool   `json:"ok"`
	UserID    int64  `json:"user_id,string"`
	Acked     int    `json:"acked"`
	Total     int    `json:"total"`
	Delivered int    `json:"delivered"`
	Detail    string `json:"detail,omitempty"`
	Error     string `json:"error,omitempty"`
}

type Stats

type Stats struct {
	Transport             string        `json:"transport"`
	MaxConnectionsPerUser int           `json:"max_connections_per_user"`
	Users                 int           `json:"users"`
	TotalConnections      int           `json:"total_connections"`
	ConnectionsByUser     map[int64]int `json:"connections_by_user"`
}

type Transport

type Transport string
const (
	TransportWS  Transport = "ws"
	TransportSSE Transport = "sse"
)

Jump to

Keyboard shortcuts

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