Documentation
¶
Index ¶
- type Client
- func (c *Client[Req, Resp]) Close()
- func (c *Client[Req, Resp]) CloseError() error
- func (c *Client[Req, Resp]) Error() <-chan error
- func (c *Client[Req, Resp]) Read() <-chan Resp
- func (c *Client[Req, Resp]) ReadMessage(ctx context.Context) (Resp, error)
- func (c *Client[Req, Resp]) Start(ctx context.Context) error
- func (c *Client[Req, Resp]) Write() chan<- Req
- type ClientConfig
- type ClientOption
- func WithClientLogger(logger *slog.Logger) ClientOption
- func WithClientReadBufferSize(size int) ClientOption
- func WithClientWriteBufferSize(size int) ClientOption
- func WithConnectRetryBackoff(d time.Duration) ClientOption
- func WithConnectTimeout(d time.Duration) ClientOption
- func WithDialOptions(opts *websocket.DialOptions) ClientOption
- func WithHTTPHeader(header http.Header) ClientOption
- func WithOnConnect(fn func(context.Context)) ClientOption
- type Config
- type Handler
- func (h *Handler[Req, Resp]) Close()
- func (h *Handler[Req, Resp]) CloseError() error
- func (h *Handler[Req, Resp]) Done() <-chan struct{}
- func (h *Handler[Req, Resp]) DrainWrites(ctx context.Context)
- func (h *Handler[Req, Resp]) Error() <-chan error
- func (h *Handler[Req, Resp]) Read() <-chan Req
- func (h *Handler[Req, Resp]) ReadMessage(ctx context.Context) (Req, error)
- func (h *Handler[Req, Resp]) Start(ctx context.Context) error
- func (h *Handler[Req, Resp]) Write() chan<- Resp
- type Option
- func WithAcceptOptions(opts *websocket.AcceptOptions) Option
- func WithInsecureSkipVerify(skip bool) Option
- func WithLogger(logger *slog.Logger) Option
- func WithOnClose(fn func(error)) Option
- func WithOriginPatterns(patterns ...string) Option
- func WithReadBufferSize(size int) Option
- func WithWriteBufferSize(size int) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client[Req, Resp any] struct { ClientConfig // contains filtered or unexported fields }
Client represents a websocket client connection with automatic reconnection support. It exposes typed read/write channels so callers never touch the raw connection.
func NewClient ¶
func NewClient[Req, Resp any](rawURL string, options ...ClientOption) (*Client[Req, Resp], error)
NewClient creates a new websocket client. Call Start to begin processing.
func (*Client[Req, Resp]) Close ¶
func (c *Client[Req, Resp]) Close()
Close gracefully closes the websocket connection.
func (*Client[Req, Resp]) CloseError ¶
CloseError returns the error that caused the client to close, if any.
func (*Client[Req, Resp]) Error ¶
Error returns a channel that receives the first fatal error, if any.
func (*Client[Req, Resp]) Read ¶
func (c *Client[Req, Resp]) Read() <-chan Resp
Read returns the channel that receives deserialized server messages.
func (*Client[Req, Resp]) ReadMessage ¶
ReadMessage blocks until a message is received, the context is cancelled, or an error occurs.
type ClientConfig ¶
type ClientConfig struct {
// contains filtered or unexported fields
}
ClientConfig holds the configuration options for a Client.
type ClientOption ¶
type ClientOption func(*ClientConfig)
ClientOption configures a Client.
func WithClientLogger ¶
func WithClientLogger(logger *slog.Logger) ClientOption
WithClientLogger sets the logger.
func WithClientReadBufferSize ¶
func WithClientReadBufferSize(size int) ClientOption
WithClientReadBufferSize sets the size of the read channel buffer.
func WithClientWriteBufferSize ¶
func WithClientWriteBufferSize(size int) ClientOption
WithClientWriteBufferSize sets the size of the write channel buffer.
func WithConnectRetryBackoff ¶
func WithConnectRetryBackoff(d time.Duration) ClientOption
WithConnectRetryBackoff sets the backoff duration between connection retry attempts.
func WithConnectTimeout ¶
func WithConnectTimeout(d time.Duration) ClientOption
WithConnectTimeout sets the timeout for initial connection attempts.
func WithDialOptions ¶
func WithDialOptions(opts *websocket.DialOptions) ClientOption
WithDialOptions sets custom dial options for the underlying coder/websocket.Dial call.
func WithHTTPHeader ¶
func WithHTTPHeader(header http.Header) ClientOption
WithHTTPHeader sets custom HTTP headers for the websocket upgrade request.
func WithOnConnect ¶
func WithOnConnect(fn func(context.Context)) ClientOption
WithOnConnect sets a callback function called when the websocket is connected (or reconnected).
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config holds the configuration options for a Handler.
type Handler ¶
Handler handles a single upgraded websocket connection. It owns the read/write goroutines and exposes typed channels so callers never touch the raw connection.
func NewHandler ¶
func NewHandler[Req, Resp any]( w http.ResponseWriter, r *http.Request, options ...Option, ) *Handler[Req, Resp]
NewHandler creates a Handler for an incoming HTTP request. Call Start to upgrade the connection and begin processing.
func (*Handler[Req, Resp]) Close ¶
func (h *Handler[Req, Resp]) Close()
Close gracefully closes this websocket connection.
func (*Handler[Req, Resp]) CloseError ¶
CloseError returns the error that caused the handler to close, if any.
func (*Handler[Req, Resp]) Done ¶
func (h *Handler[Req, Resp]) Done() <-chan struct{}
Done returns a channel that is closed when the handler is shut down.
func (*Handler[Req, Resp]) DrainWrites ¶
DrainWrites signals the write goroutine to flush remaining buffered messages and exit. The write channel is never closed, so concurrent senders cannot panic.
func (*Handler[Req, Resp]) Error ¶
Error returns a channel that receives the first fatal error, if any.
func (*Handler[Req, Resp]) Read ¶
func (h *Handler[Req, Resp]) Read() <-chan Req
Read returns the channel that receives deserialized client messages.
func (*Handler[Req, Resp]) ReadMessage ¶
ReadMessage blocks until a message is received, the context is cancelled, or an error occurs.
type Option ¶
type Option func(*Config)
Option configures a Handler.
func WithAcceptOptions ¶
func WithAcceptOptions(opts *websocket.AcceptOptions) Option
WithAcceptOptions sets the full AcceptOptions for the underlying coder/websocket.Accept call.
func WithInsecureSkipVerify ¶
WithInsecureSkipVerify controls whether origin checking is skipped during the websocket upgrade. Defaults to true (allow all origins) to match common dev usage.
func WithOnClose ¶
WithOnClose sets the callback function called when the handler closes. The callback receives any error that caused the closure, or nil for a clean close.
func WithOriginPatterns ¶
WithOriginPatterns sets allowed origin patterns for the websocket upgrade. Only relevant when InsecureSkipVerify is false.
func WithReadBufferSize ¶
WithReadBufferSize sets the size of the read channel buffer.
func WithWriteBufferSize ¶
WithWriteBufferSize sets the size of the write channel buffer.