Documentation
¶
Overview ¶
Package client provides a robust, mTLS-capable WebSocket client for WSBroker with automatic reconnection, dynamic channel subscription tracking, and event routing.
Index ¶
- type Client
- func (c *Client) Attach(ctx context.Context, channels ...string) error
- func (c *Client) Broadcast(ctx context.Context, channel string, data any) error
- func (c *Client) Channels() []string
- func (c *Client) ClientName() string
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) ConnectSync(ctx context.Context) error
- func (c *Client) Detach(ctx context.Context, channels ...string) error
- func (c *Client) GetState() ConnectionState
- func (c *Client) IsConnected() bool
- func (c *Client) OnChannel(channel string, handler EventHandler) UnsubscribeFunc
- func (c *Client) OnConnect(callback func()) UnsubscribeFunc
- func (c *Client) OnDirect(handler EventHandler) UnsubscribeFunc
- func (c *Client) OnDisconnect(callback func(error)) UnsubscribeFunc
- func (c *Client) OnError(callback func(error)) UnsubscribeFunc
- func (c *Client) OnEvent(handler EventHandler) UnsubscribeFunc
- func (c *Client) OnMessage(handler EventHandler) UnsubscribeFunc
- func (c *Client) OnReconnect(callback func()) UnsubscribeFunc
- func (c *Client) OnType(t event.EventType, handler EventHandler) UnsubscribeFunc
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Send(ctx context.Context, evt *event.Event) error
- func (c *Client) SendDirect(ctx context.Context, targetClient string, data any) error
- func (c *Client) SendMessage(ctx context.Context, target string, data any) error
- func (c *Client) Subscribe(ctx context.Context, channel string) (Subscription, error)
- type Config
- type ConnectionState
- type EventHandler
- type Option
- func WithAutoReconnect(enabled bool) Option
- func WithBearerToken(token string) Option
- func WithClientName(name string) Option
- func WithConnectTimeout(timeout time.Duration) Option
- func WithEndpoint(endpoint string) Option
- func WithInitialChannels(channels ...string) Option
- func WithLogger(logger zLogger.ZLogger) Option
- func WithPingInterval(interval time.Duration) Option
- func WithReconnectStrategy(initial, max time.Duration, factor float64, maxAttempts int) Option
- func WithTLSCertLoader(certLoaderConfig *loader.Config) Option
- func WithTLSConfig(tlsConfig *tls.Config) Option
- func WithWriteTimeout(timeout time.Duration) Option
- type Subscription
- type UnsubscribeFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the primary interface for communicating with a WSBroker server.
func (*Client) ClientName ¶
ClientName returns the registered client identifier.
func (*Client) Close ¶
Close gracefully closes the WebSocket connection and releases background goroutines.
func (*Client) Connect ¶
Connect opens the WebSocket connection to WSBroker. If AutoReconnect is true, it launches background reconnect monitoring on disconnection.
func (*Client) ConnectSync ¶
ConnectSync attempts an initial connection and blocks until connected or ctx is cancelled. If the initial connection fails, an error is returned immediately without entering background reconnect.
func (*Client) GetState ¶
func (c *Client) GetState() ConnectionState
GetState returns the current connection lifecycle state.
func (*Client) IsConnected ¶
IsConnected returns true if the client is actively connected to the server.
func (*Client) OnChannel ¶
func (c *Client) OnChannel(channel string, handler EventHandler) UnsubscribeFunc
OnChannel registers a listener for events targeted to a specific channel.
func (*Client) OnConnect ¶
func (c *Client) OnConnect(callback func()) UnsubscribeFunc
OnConnect registers a callback invoked when the client successfully connects.
func (*Client) OnDirect ¶
func (c *Client) OnDirect(handler EventHandler) UnsubscribeFunc
OnDirect registers a listener for direct point-to-point messages sent to this client.
func (*Client) OnDisconnect ¶
func (c *Client) OnDisconnect(callback func(error)) UnsubscribeFunc
OnDisconnect registers a callback invoked when the connection is dropped.
func (*Client) OnError ¶
func (c *Client) OnError(callback func(error)) UnsubscribeFunc
OnError registers a callback invoked when an error occurs during connection or reconnect.
func (*Client) OnEvent ¶
func (c *Client) OnEvent(handler EventHandler) UnsubscribeFunc
OnEvent registers a global event listener called for every received event.
func (*Client) OnMessage ¶
func (c *Client) OnMessage(handler EventHandler) UnsubscribeFunc
OnMessage registers a listener for incoming TypeMessage events.
func (*Client) OnReconnect ¶
func (c *Client) OnReconnect(callback func()) UnsubscribeFunc
OnReconnect registers a callback invoked after a successful reconnection.
func (*Client) OnType ¶
func (c *Client) OnType(t event.EventType, handler EventHandler) UnsubscribeFunc
OnType registers a listener for events of a specific EventType.
func (*Client) SendDirect ¶
SendDirect sends a direct message to a named client.
func (*Client) SendMessage ¶
SendMessage sends a payload to a target (channel or direct client).
type Config ¶
type Config struct {
// Endpoint is the target WebSocket URL (e.g. "wss://localhost:8080/ws/client-1" or "http://localhost:8080").
Endpoint string `json:"endpoint" toml:"endpoint"`
// ClientName is the unique client identifier registered with WSBroker.
// If omitted, a random client ID is generated or extracted from Endpoint URL.
ClientName string `json:"client_name" toml:"client_name"`
// InitialChannels is the list of channel names to subscribe to upon initial connection.
InitialChannels []string `json:"initial_channels" toml:"initial_channels"`
// Bearer is the optional Bearer token used for HTTP/WS authentication.
Bearer string `json:"bearer" toml:"bearer"`
// TLSConfig is a custom standard crypto/tls configuration.
TLSConfig *tls.Config `json:"-" toml:"-"`
// TLSCertLoader is the certloader configuration for dynamic mTLS and certificate rotation.
TLSCertLoader *loader.Config `json:"tls" toml:"tls"`
// AutoReconnect enables automatic reconnection upon unexpected disconnection.
AutoReconnect bool `json:"auto_reconnect" toml:"auto_reconnect"`
// ReconnectInterval is the initial wait duration before the first reconnection attempt.
ReconnectInterval time.Duration `json:"reconnect_interval" toml:"reconnect_interval"`
// MaxReconnectInterval is the upper bound duration for exponential backoff.
MaxReconnectInterval time.Duration `json:"max_reconnect_interval" toml:"max_reconnect_interval"`
// ReconnectBackoffFactor is the multiplier applied to backoff after each failed attempt.
ReconnectBackoffFactor float64 `json:"reconnect_backoff_factor" toml:"reconnect_backoff_factor"`
// MaxReconnectAttempts is the maximum number of reconnect attempts before giving up (0 = infinite).
MaxReconnectAttempts int `json:"max_reconnect_attempts" toml:"max_reconnect_attempts"`
// PingInterval is the interval at which client-side heartbeat pings are sent (0 = disabled).
PingInterval time.Duration `json:"ping_interval" toml:"ping_interval"`
// ConnectTimeout is the timeout duration for establishing a connection.
ConnectTimeout time.Duration `json:"connect_timeout" toml:"connect_timeout"`
// WriteTimeout is the timeout duration for write operations.
WriteTimeout time.Duration `json:"write_timeout" toml:"write_timeout"`
// Logger is the logger instance used for diagnostics and debug logging.
Logger zLogger.ZLogger `json:"-" toml:"-"`
}
Config defines the configuration parameters for initializing a WSBroker Client.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config initialized with sensible default values.
type ConnectionState ¶
type ConnectionState string
ConnectionState represents the current lifecycle state of the WebSocket client.
const ( // StateDisconnected indicates the client is currently not connected. StateDisconnected ConnectionState = "disconnected" // StateConnecting indicates a connection attempt is actively in progress. StateConnecting ConnectionState = "connecting" // StateConnected indicates the WebSocket connection is open and active. StateConnected ConnectionState = "connected" // StateReconnecting indicates the client is attempting to re-establish a dropped connection. StateReconnecting ConnectionState = "reconnecting" // StateClosed indicates the client has been permanently closed via Close(). StateClosed ConnectionState = "closed" )
type EventHandler ¶
EventHandler is a callback function invoked when a matching event is received.
type Option ¶
type Option func(*Config)
Option is a function that modifies Config.
func WithAutoReconnect ¶
WithAutoReconnect enables or disables automatic reconnection.
func WithBearerToken ¶
WithBearerToken sets the Bearer authentication token.
func WithClientName ¶
WithClientName sets the client identifier.
func WithConnectTimeout ¶
WithConnectTimeout sets the timeout for establishing connections.
func WithEndpoint ¶
WithEndpoint sets the WebSocket target endpoint.
func WithInitialChannels ¶
WithInitialChannels specifies initial channels to subscribe to on connection.
func WithLogger ¶
WithLogger sets the logger instance.
func WithPingInterval ¶
WithPingInterval sets the interval for heartbeat pings.
func WithReconnectStrategy ¶
WithReconnectStrategy configures the reconnection backoff parameters.
func WithTLSCertLoader ¶
WithTLSCertLoader configures certloader for dynamic mTLS certificates.
func WithTLSConfig ¶
WithTLSConfig configures standard crypto/tls TLSConfig.
func WithWriteTimeout ¶
WithWriteTimeout sets the timeout for write operations.
type Subscription ¶
type Subscription interface {
// Channel returns the subscribed channel name.
Channel() string
// Events returns the receive-only channel carrying incoming events.
Events() <-chan *event.Event
// Unsubscribe stops the subscription and releases resources.
Unsubscribe() error
}
Subscription represents an active channel subscription stream.
type UnsubscribeFunc ¶
type UnsubscribeFunc func()
UnsubscribeFunc is a function that removes a registered listener or subscription when called.