realtime

package
v0.1.3 Latest Latest
Warning

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

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

Documentation

Overview

Package realtime 提供共享身份入口和基于 Redis Stream 的跨实例恢复.

Index

Constants

This section is empty.

Variables

View Source
var ErrResumeInvalid = errors.New("恢复会话无效,请重新建立连接")

ErrResumeInvalid表示实时恢复凭证失效,不代表账号登录失效。

Functions

This section is empty.

Types

type Handler

type Handler func(context.Context, Principal, json.RawMessage) (any, error)

type Hub

type Hub struct {
	Redis     redis.UniversalClient
	Prefix    string
	Path      string
	Origins   []string
	Validate  func(context.Context, Principal) error
	Authorize func(context.Context, Principal, string) error
	// AuthorizeEvent checks permissions again before an inbox or channel event is delivered.
	AuthorizeEvent func(context.Context, Principal, Message) error
	// Connected runs after authentication; Disconnected runs when that connection exits.
	Connected    func(context.Context, Principal) error
	Disconnected func(context.Context, Principal)
	Retention    time.Duration
	Limit        int64
	// contains filtered or unexported fields
}

func New

func New(client redis.UniversalClient, prefix, path string) *Hub

func (*Hub) Close

func (h *Hub) Close()

func (*Hub) Drain

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

Drain 停接新连接并分散通知旧连接重连,超时后回收剩余连接。

func (*Hub) Handle

func (h *Hub) Handle(name string, handler Handler) error

Handle 注册普通业务函数, 无动态插件或自动扫描.

func (*Hub) Presence added in v0.1.3

func (h *Hub) Presence(ctx context.Context, p Principal) (int64, error)

Presence 返回身份下最近有效连接的活动时间,所有标签页与实例共享租约。

func (*Hub) Publish

func (h *Hub) Publish(ctx context.Context, to Principal, event string, data any) (string, error)

Publish 把事件存入接收者收件流; 每个设备独立维护确认位置.

func (*Hub) PublishChannel

func (h *Hub) PublishChannel(ctx context.Context, channel, event string, data any) (string, error)

func (*Hub) ServeHTTP

func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Hub) Ticket

func (h *Hub) Ticket(ctx context.Context, p Principal, resume, locale string) (string, error)

Ticket 只能由已完成HTTP身份验证的业务签发, 恢复凭证绑定原身份和登录会话.

type Message

type Message struct {
	Type      string          `json:"type"`
	ID        string          `json:"id,omitempty"`
	RequestID string          `json:"request_id,omitempty"`
	Channel   string          `json:"channel,omitempty"`
	Ticket    string          `json:"ticket,omitempty"`
	Data      json.RawMessage `json:"data,omitempty"`
}

type Principal

type Principal struct {
	Realm     string `json:"realm"`
	Subject   string `json:"subject"`
	SessionID string `json:"session_id"`
}

Jump to

Keyboard shortcuts

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