Documentation
¶
Index ¶
- func LogConnection(logger *zerolog.Logger, url string, connected bool)
- func LogReconnect(logger *zerolog.Logger, url string, attempt int, err error)
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) IsConnected() bool
- func (c *Client) Receive(ctx context.Context) (*Message, error)
- func (c *Client) RegisterHandler(messageType string, handler HandlerFunc)
- func (c *Client) Send(ctx context.Context, message interface{}) error
- func (c *Client) SendRaw(ctx context.Context, data []byte) error
- func (c *Client) UnregisterHandler(messageType string)
- func (c *Client) WithLogging(logger *zerolog.Logger) *Client
- func (c *Client) WithMetrics(namespace string) *Client
- func (c *Client) WithRetry(maxAttempts int, initialDelay time.Duration) *Client
- type Conn
- type HandlerFunc
- type HandlerRegistry
- type Message
- type Pool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogConnection ¶
LogConnection logs connection events.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides WebSocket client functionality with auto-reconnect, message handling, and connection pooling.
func (*Client) Close ¶
Close gracefully closes the WebSocket client and all connections. It waits for in-flight messages to be processed before closing.
func (*Client) Connect ¶
Connect establishes a WebSocket connection to the configured URL. It will automatically retry on failure according to the reconnect configuration.
func (*Client) IsConnected ¶
IsConnected returns true if the client has an active connection.
func (*Client) Receive ¶
Receive blocks until a message is received or the context is canceled. It returns the raw message data.
func (*Client) RegisterHandler ¶
func (c *Client) RegisterHandler(messageType string, handler HandlerFunc)
RegisterHandler registers a message handler for a specific message type. When a message with the matching type is received, the handler will be called.
func (*Client) Send ¶
Send sends a message to the WebSocket server. The message is JSON-encoded before sending.
func (*Client) UnregisterHandler ¶
UnregisterHandler removes a message handler for a specific message type.
func (*Client) WithLogging ¶
WithLogging adds logging middleware to the WebSocket client. It logs connection events, disconnections, and message activity.
func (*Client) WithMetrics ¶
WithMetrics adds metrics middleware to the WebSocket client. It records message count, connection duration, and reconnect attempts.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a single WebSocket connection with auto-reconnect and ping/pong support.
func (*Conn) IsConnected ¶
IsConnected returns true if the connection is active.
type HandlerFunc ¶
HandlerFunc is a function that processes WebSocket messages.
type HandlerRegistry ¶
type HandlerRegistry struct {
// contains filtered or unexported fields
}
HandlerRegistry manages message handlers for different message types.
func NewHandlerRegistry ¶
func NewHandlerRegistry() *HandlerRegistry
NewHandlerRegistry creates a new handler registry.
func (*HandlerRegistry) Count ¶
func (r *HandlerRegistry) Count() int
Count returns the number of registered handlers.
func (*HandlerRegistry) Handle ¶
func (r *HandlerRegistry) Handle(ctx context.Context, msg *Message) error
Handle dispatches a message to the appropriate handler based on its type.
func (*HandlerRegistry) Register ¶
func (r *HandlerRegistry) Register(messageType string, handler HandlerFunc)
Register registers a handler for a specific message type.
func (*HandlerRegistry) Unregister ¶
func (r *HandlerRegistry) Unregister(messageType string)
Unregister removes a handler for a specific message type.
type Message ¶
type Message struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
Raw []byte
}
Message represents a WebSocket message with type information.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool manages multiple WebSocket connections for load distribution and redundancy.
func (*Pool) GetHealthy ¶
GetHealthy returns a healthy connection, skipping disconnected ones.
func (*Pool) HealthyCount ¶
HealthyCount returns the number of connected connections in the pool.