websocket

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

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

func (c *Client[Req, Resp]) CloseError() error

CloseError returns the error that caused the client to close, if any.

func (*Client[Req, Resp]) Error

func (c *Client[Req, Resp]) Error() <-chan 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

func (c *Client[Req, Resp]) ReadMessage(ctx context.Context) (Resp, error)

ReadMessage blocks until a message is received, the context is cancelled, or an error occurs.

func (*Client[Req, Resp]) Start

func (c *Client[Req, Resp]) Start(ctx context.Context) error

Start establishes the initial connection and spawns background read/write goroutines.

func (*Client[Req, Resp]) Write

func (c *Client[Req, Resp]) Write() chan<- Req

Write returns the channel used to send messages to the server.

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

type Handler[Req, Resp any] struct {
	Config
	// contains filtered or unexported fields
}

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

func (h *Handler[Req, Resp]) CloseError() error

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

func (h *Handler[Req, Resp]) DrainWrites(ctx context.Context)

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

func (h *Handler[Req, Resp]) Error() <-chan 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

func (h *Handler[Req, Resp]) ReadMessage(ctx context.Context) (Req, error)

ReadMessage blocks until a message is received, the context is cancelled, or an error occurs.

func (*Handler[Req, Resp]) Start

func (h *Handler[Req, Resp]) Start(ctx context.Context) error

Start upgrades the HTTP connection to a websocket and spawns background read/write goroutines.

func (*Handler[Req, Resp]) Write

func (h *Handler[Req, Resp]) Write() chan<- Resp

Write returns the channel used to send messages to the client.

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

func WithInsecureSkipVerify(skip bool) Option

WithInsecureSkipVerify controls whether origin checking is skipped during the websocket upgrade. Defaults to true (allow all origins) to match common dev usage.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger.

func WithOnClose

func WithOnClose(fn func(error)) Option

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

func WithOriginPatterns(patterns ...string) Option

WithOriginPatterns sets allowed origin patterns for the websocket upgrade. Only relevant when InsecureSkipVerify is false.

func WithReadBufferSize

func WithReadBufferSize(size int) Option

WithReadBufferSize sets the size of the read channel buffer.

func WithWriteBufferSize

func WithWriteBufferSize(size int) Option

WithWriteBufferSize sets the size of the write channel buffer.

Jump to

Keyboard shortcuts

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