ws

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithClientConnectOption

func WithClientConnectOption(opts ...opt.Option[connectOptions]) opt.Option[clientOptions]

func WithClientDialTimeout

func WithClientDialTimeout(d time.Duration) opt.Option[clientOptions]

func WithClientHTTPHeader

func WithClientHTTPHeader(header http.Header) opt.Option[clientOptions]

func WithClientMaxReconnectAttempts

func WithClientMaxReconnectAttempts(n int) opt.Option[clientOptions]

func WithClientReconnectInterval

func WithClientReconnectInterval(d time.Duration) opt.Option[clientOptions]

func WithHeartbeatInterval

func WithHeartbeatInterval(d time.Duration) opt.Option[connectOptions]

func WithHubBroker

func WithHubBroker(broker MessageBroker) opt.Option[hubOptions]

func WithHubChannel

func WithHubChannel(channel string) opt.Option[hubOptions]

func WithReadTimeout

func WithReadTimeout(d time.Duration) opt.Option[connectOptions]

func WithServerBeforeUpgrade

func WithServerBeforeUpgrade(f func(r *http.Request) error) opt.Option[serverOptions]

func WithServerCheckOrigin

func WithServerCheckOrigin(f func(r *http.Request) bool) opt.Option[serverOptions]

func WithServerConnectOption

func WithServerConnectOption(opts ...opt.Option[connectOptions]) opt.Option[serverOptions]

func WithServerOnConnect

func WithServerOnConnect(f func(c Connect, r *http.Request) error) opt.Option[serverOptions]

func WithServerPath

func WithServerPath(path string) opt.Option[serverOptions]

func WithServerReadBufferSize

func WithServerReadBufferSize(size int) opt.Option[serverOptions]

func WithServerWriteBufferSize

func WithServerWriteBufferSize(size int) opt.Option[serverOptions]

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) opt.Option[connectOptions]

Types

type Client

type Client interface {
	Connect(context.Context) error
	Close()
	OnConnect(func(Connect))
	OnMessage(func(websocket.MessageType, []byte))
	OnClose(func(error))
}

func NewClient

func NewClient(addr string, opts ...opt.Option[clientOptions]) Client

type Connect

type Connect interface {
	// SendText queues a text message. It returns nil if queued, error if closed.
	// Context is used for queuing timeout if channel is full.
	SendText(context.Context, string) error

	// SendBinary queues a binary message.
	SendBinary(context.Context, []byte) error

	// SendJSON queues a JSON message.
	SendJSON(context.Context, interface{}) error

	// ReadMessage blocks until a message is received or context done.
	ReadMessage(context.Context) (websocket.MessageType, []byte, error)

	// Close closes the connection and loops.
	Close() error

	// Conn returns underlying connection (use with caution).
	Conn() *websocket.Conn

	// ID returns the unique identifier for this connection
	ID() string
	// SetID sets the unique identifier
	SetID(string)

	// Get retrieves a value from metadata
	Get(key string) (value interface{}, exists bool)
	// Set stores a value in metadata
	Set(key string, value interface{})
}

func NewConnect

func NewConnect(conn *websocket.Conn, opts ...opt.Option[connectOptions]) Connect

type Hub

type Hub interface {
	// Register 注册连接
	Register(Connect)
	// Unregister 注销连接
	Unregister(Connect)

	// Broadcast 广播消息给所有连接 (分布式)
	Broadcast(msg []byte)

	// SendTo 向特定 UserID 的连接发送消息 (分布式)
	SendTo(userID string, msg []byte)

	// Count 返回当前在线连接数 (本地)
	Count() int64

	// Close 关闭所有连接
	Close()
}

Hub 管理所有活跃连接,支持广播和单播

func NewHub

func NewHub(opts ...opt.Option[hubOptions]) Hub

type MessageBroker

type MessageBroker interface {
	// Subscribe 订阅频道,当收到消息时调用 handler
	Subscribe(ctx context.Context, channel string, handler func(msg []byte)) error
	// Publish 发布消息到频道
	Publish(ctx context.Context, channel string, msg []byte) error
	// Close 关闭代理连接
	Close() error
}

MessageBroker 定义消息代理接口,用于跨节点通信

type RedisBroker

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

func NewRedisBroker

func NewRedisBroker(addr string, password string, db int) *RedisBroker

func (*RedisBroker) Close

func (b *RedisBroker) Close() error

func (*RedisBroker) Publish

func (b *RedisBroker) Publish(ctx context.Context, channel string, msg []byte) error

func (*RedisBroker) Subscribe

func (b *RedisBroker) Subscribe(ctx context.Context, channel string, handler func(msg []byte)) error

type Server

type Server interface {
	Start(func(Connect)) error
	Shutdown(context.Context) error
	Handler(func(Connect)) http.HandlerFunc
}

func NewServer

func NewServer(conf *ServerConfig, opts ...opt.Option[serverOptions]) Server

type ServerConfig

type ServerConfig struct {
	Addr string
}

Jump to

Keyboard shortcuts

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