Documentation
¶
Overview ¶
Package wsreconnect provides a resilient, auto-reconnecting WebSocket client with exponential backoff, ping/pong heartbeats, and lifecycle event hooks.
Index ¶
- Variables
- type BackoffConfig
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) IsClosed() bool
- func (c *Client) IsConnected() bool
- func (c *Client) ReconnectCount() int64
- func (c *Client) SendBinary(data []byte) error
- func (c *Client) SendJSON(v any) error
- func (c *Client) SendText(text string) error
- type Config
- type Option
- func WithBackoff(cfg BackoffConfig) Option
- func WithBufferSize(readSize, writeSize int) Option
- func WithHeaders(headers http.Header) Option
- func WithOnBinaryMessage(fn func(msg []byte)) Option
- func WithOnConnect(fn func()) Option
- func WithOnDisconnect(fn func(err error)) Option
- func WithOnError(fn func(err error)) Option
- func WithOnTextMessage(fn func(msg []byte)) Option
- func WithPingInterval(d time.Duration) Option
- func WithPongWait(d time.Duration) Option
- func WithSubprotocols(subprotocols ...string) Option
- func WithWriteChanSize(size int) Option
- func WithWriteWait(d time.Duration) Option
Constants ¶
This section is empty.
Variables ¶
var ( ErrConnectionClosed = errors.New("wsreconnect: client is closed") ErrNotConnected = errors.New("wsreconnect: client is not connected") ErrWriteBufferFull = errors.New("wsreconnect: outbound write buffer is full") ErrMaxRetriesExceeded = errors.New("wsreconnect: maximum reconnection retries exceeded") )
Sentinel errors.
Functions ¶
This section is empty.
Types ¶
type BackoffConfig ¶
type BackoffConfig struct {
// InitialInterval is the initial wait time before the first retry attempt.
// Default is 500ms.
InitialInterval time.Duration
// MaxInterval is the maximum wait time between retries.
// Default is 30s.
MaxInterval time.Duration
// Multiplier is the factor by which the retry interval increases.
// Default is 1.5.
Multiplier float64
// Jitter enables randomized interval fluctuation to prevent thundering herd.
// Default is true.
Jitter bool
// MaxRetries is the maximum number of reconnection attempts.
// 0 means unlimited retries. Default is 0.
MaxRetries int
}
BackoffConfig defines exponential backoff parameters for auto-reconnection.
func DefaultBackoffConfig ¶
func DefaultBackoffConfig() BackoffConfig
DefaultBackoffConfig returns the default backoff configuration.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a resilient WebSocket client that automatically reconnects on disconnects.
func (*Client) Close ¶
Close gracefully closes the WebSocket connection and terminates the background loops.
func (*Client) Connect ¶
Connect starts the client, connects to the server, and maintains auto-reconnection in the background. It returns when the initial connection is established, or returns an error if the context is canceled.
func (*Client) IsConnected ¶
IsConnected reports whether the WebSocket client is currently connected.
func (*Client) ReconnectCount ¶
ReconnectCount returns the total number of reconnection attempts made.
func (*Client) SendBinary ¶
SendBinary sends raw binary data to the server asynchronously and thread-safely.
type Config ¶
type Config struct {
Backoff BackoffConfig
PingInterval time.Duration
PongWait time.Duration
WriteWait time.Duration
ReadBufferSize int
WriteBufferSize int
WriteChanSize int
Headers http.Header
Subprotocols []string
OnConnect func()
OnDisconnect func(err error)
OnTextMessage func(msg []byte)
OnBinaryMessage func(msg []byte)
OnError func(err error)
}
Config holds client configurations.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns default client configuration.
type Option ¶
type Option func(*Config)
Option configures the Client.
func WithBackoff ¶
func WithBackoff(cfg BackoffConfig) Option
WithBackoff sets the backoff retry configuration.
func WithBufferSize ¶
WithBufferSize configures read and write buffer sizes for the connection.
func WithHeaders ¶
WithHeaders sets the HTTP headers sent during the WebSocket handshake.
func WithOnBinaryMessage ¶
WithOnBinaryMessage registers a callback for received binary messages.
func WithOnConnect ¶
func WithOnConnect(fn func()) Option
WithOnConnect registers a callback invoked when connection is established.
func WithOnDisconnect ¶
WithOnDisconnect registers a callback invoked when connection is lost.
func WithOnError ¶
WithOnError registers a callback for general errors.
func WithOnTextMessage ¶
WithOnTextMessage registers a callback for received text messages.
func WithPingInterval ¶
WithPingInterval sets the interval between sending ping frames to the server.
func WithPongWait ¶
WithPongWait sets how long to wait for a pong response from the server.
func WithSubprotocols ¶
WithSubprotocols specifies supported subprotocols.
func WithWriteChanSize ¶
WithWriteChanSize sets the buffer capacity of the outbound message channel.
func WithWriteWait ¶
WithWriteWait sets the deadline for writing a message to the WebSocket.