Documentation
¶
Index ¶
- func WithClientConnectOption(opts ...opt.Option[connectOptions]) opt.Option[clientOptions]
- func WithClientDialTimeout(d time.Duration) opt.Option[clientOptions]
- func WithClientHTTPHeader(header http.Header) opt.Option[clientOptions]
- func WithClientMaxReconnectAttempts(n int) opt.Option[clientOptions]
- func WithClientReconnectInterval(d time.Duration) opt.Option[clientOptions]
- func WithHeartbeatInterval(d time.Duration) opt.Option[connectOptions]
- func WithHubBroker(broker MessageBroker) opt.Option[hubOptions]
- func WithHubChannel(channel string) opt.Option[hubOptions]
- func WithReadTimeout(d time.Duration) opt.Option[connectOptions]
- func WithServerBeforeUpgrade(f func(r *http.Request) error) opt.Option[serverOptions]
- func WithServerCheckOrigin(f func(r *http.Request) bool) opt.Option[serverOptions]
- func WithServerConnectOption(opts ...opt.Option[connectOptions]) opt.Option[serverOptions]
- func WithServerOnConnect(f func(c Connect, r *http.Request) error) opt.Option[serverOptions]
- func WithServerPath(path string) opt.Option[serverOptions]
- func WithServerReadBufferSize(size int) opt.Option[serverOptions]
- func WithServerWriteBufferSize(size int) opt.Option[serverOptions]
- func WithWriteTimeout(d time.Duration) opt.Option[connectOptions]
- type Client
- type Connect
- type Hub
- type MessageBroker
- type RedisBroker
- type Server
- type ServerConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithClientConnectOption ¶
func WithHubBroker ¶
func WithHubBroker(broker MessageBroker) opt.Option[hubOptions]
func WithHubChannel ¶
func WithServerBeforeUpgrade ¶
func WithServerCheckOrigin ¶
func WithServerConnectOption ¶
func WithServerOnConnect ¶
func WithServerPath ¶
Types ¶
type 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{})
}
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 管理所有活跃连接,支持广播和单播
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
type Server ¶
type ServerConfig ¶
type ServerConfig struct {
Addr string
}
Click to show internal directories.
Click to hide internal directories.