websocket

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogConnection

func LogConnection(logger *zerolog.Logger, url string, connected bool)

LogConnection logs connection events.

func LogReconnect

func LogReconnect(logger *zerolog.Logger, url string, attempt int, err error)

LogReconnect logs reconnection attempts.

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 New

New creates a new WebSocket client with the provided configuration.

func (*Client) Close

func (c *Client) Close() error

Close gracefully closes the WebSocket client and all connections. It waits for in-flight messages to be processed before closing.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect establishes a WebSocket connection to the configured URL. It will automatically retry on failure according to the reconnect configuration.

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected returns true if the client has an active connection.

func (*Client) Receive

func (c *Client) Receive(ctx context.Context) (*Message, error)

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

func (c *Client) Send(ctx context.Context, message interface{}) error

Send sends a message to the WebSocket server. The message is JSON-encoded before sending.

func (*Client) SendRaw

func (c *Client) SendRaw(ctx context.Context, data []byte) error

SendRaw sends raw bytes to the WebSocket server.

func (*Client) UnregisterHandler

func (c *Client) UnregisterHandler(messageType string)

UnregisterHandler removes a message handler for a specific message type.

func (*Client) WithLogging

func (c *Client) WithLogging(logger *zerolog.Logger) *Client

WithLogging adds logging middleware to the WebSocket client. It logs connection events, disconnections, and message activity.

func (*Client) WithMetrics

func (c *Client) WithMetrics(namespace string) *Client

WithMetrics adds metrics middleware to the WebSocket client. It records message count, connection duration, and reconnect attempts.

func (*Client) WithRetry

func (c *Client) WithRetry(maxAttempts int, initialDelay time.Duration) *Client

WithRetry adds retry middleware to the WebSocket client. It automatically retries failed operations with exponential backoff.

type Conn

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

Conn represents a single WebSocket connection with auto-reconnect and ping/pong support.

func NewConn

func NewConn(ctx context.Context, cfg config.WebSocketConfig) (*Conn, error)

NewConn creates a new WebSocket connection with auto-reconnect support.

func (*Conn) Close

func (c *Conn) Close() error

Close gracefully closes the WebSocket connection.

func (*Conn) IsConnected

func (c *Conn) IsConnected() bool

IsConnected returns true if the connection is active.

func (*Conn) Receive

func (c *Conn) Receive(ctx context.Context) (int, []byte, error)

Receive reads a message from the WebSocket server.

func (*Conn) Send

func (c *Conn) Send(ctx context.Context, messageType int, data []byte) error

Send sends a message to the WebSocket server.

type HandlerFunc

type HandlerFunc func(context.Context, *Message) error

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 NewPool

func NewPool(ctx context.Context, cfg config.WebSocketConfig) (*Pool, error)

NewPool creates a new connection pool with the specified size.

func (*Pool) Close

func (p *Pool) Close() error

Close closes all connections in the pool.

func (*Pool) Connect

func (p *Pool) Connect(ctx context.Context) error

Connect establishes all connections in the pool.

func (*Pool) Get

func (p *Pool) Get() *Conn

Get returns the next available connection using round-robin distribution.

func (*Pool) GetHealthy

func (p *Pool) GetHealthy() *Conn

GetHealthy returns a healthy connection, skipping disconnected ones.

func (*Pool) HealthyCount

func (p *Pool) HealthyCount() int

HealthyCount returns the number of connected connections in the pool.

func (*Pool) Size

func (p *Pool) Size() int

Size returns the number of connections in the pool.

Jump to

Keyboard shortcuts

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