coder

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package coder provides a configurable WebSocket client. It leverages the coder/websocket library for underlying WebSocket functionality and is designed for concurrent safety.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Errors - Connection.
	ErrNotConnected     = errors.New("gows/coder: websocket client is not connected")
	ErrAlreadyConnected = errors.New("gows/coder: websocket client is already connected")
	ErrNotListening     = errors.New("gows/coder: websocket client is not listening")
	ErrAlreadyListening = errors.New("gows/coder: websocket client is already listening")

	// Errors - Events.
	ErrInvalidEventType = errors.New("gows/coder: invalid event type")

	// Errors - Callbacks.
	ErrNoOnConnect = errors.New("gows/coder: OnConnect callback is not configured")
	ErrNoOnClose   = errors.New("gows/coder: OnClose callback is not configured")
	ErrNoOnMessage = errors.New("gows/coder: OnMessage callback is not configured")
	ErrNoOnError   = errors.New("gows/coder: OnError callback is not configured")
)

Functions

This section is empty.

Types

type Client

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

Client is a thread-safe WebSocket client wrapper for coder/websocket. It allows for one concurrent reader and multiple concurrent writers.

func New

func New(cfg *Config) *Client

New creates a new WebSocket client.

func (*Client) Close

func (c *Client) Close() error

Close gracefully closes the WebSocket connection. It's safe to call Close multiple times.

func (*Client) Connect

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

Connect establishes a WebSocket connection.

func (*Client) HandshakeResponse

func (c *Client) HandshakeResponse() *http.Response

HandshakeResponse returns the HTTP response from the initial WebSocket handshake. It can be useful for inspecting headers, cookies, or the status code. The response is nil if the client has not connected yet.

func (*Client) IsListening added in v0.3.0

func (c *Client) IsListening() bool

IsListening returns whether the client is currently listening for messages.

func (*Client) On added in v0.3.0

func (c *Client) On(eventType gows.EventType, callback any) error

Supported event types: EventConnect, EventClose, EventMessage, EventError.

func (*Client) Read

func (c *Client) Read(ctx context.Context, v any) (gows.MessageType, []byte, error)

Read reads a single message. It directly uses the underlying library's convenience method for simplicity and efficiency. IMPORTANT: Your application should have only ONE goroutine calling Read.

func (*Client) Reader

func (c *Client) Reader(ctx context.Context) (gows.MessageType, io.Reader, error)

Reader returns a streaming reader for the next message. This is useful for very large messages that shouldn't be loaded into memory at once. IMPORTANT: Your application should have only ONE goroutine calling Reader.

func (*Client) StartListening added in v0.3.0

func (c *Client) StartListening() error

StartListening starts a background goroutine that continuously listens for incoming messages and calls the OnMessage callback for each received message. This method should be called after Connect() and will return an error if not connected. Only one listening goroutine can be active at a time.

func (*Client) StopListening added in v0.3.0

func (c *Client) StopListening()

StopListening stops the background message listening goroutine.

func (*Client) Write

func (c *Client) Write(ctx context.Context, typ gows.MessageType, p []byte) error

Write writes a single message. It is safe for concurrent use by multiple goroutines.

func (*Client) Writer

func (c *Client) Writer(ctx context.Context, typ gows.MessageType) (io.WriteCloser, error)

Writer returns a streaming writer for a new message.

type Config

type Config struct {
	Context     context.Context
	URL         string
	Heartbeat   time.Duration
	ReadLimit   int64
	DialOptions *websocket.DialOptions

	Listening bool // Automatically start listening for messages after connection
	OnConnect func()
	OnClose   func()
	OnMessage func(gows.MessageType, []byte)
	OnError   func(error)

	Logger golog.Logger
}

Config holds the configuration for the client.

func DefaultConfig added in v0.4.0

func DefaultConfig() *Config

func NewConfig added in v0.4.0

func NewConfig() *Config

func (*Config) Clone added in v0.4.0

func (c *Config) Clone() *Config

func (*Config) WithContext added in v0.4.0

func (c *Config) WithContext(ctx context.Context) *Config

func (*Config) WithDialOptions added in v0.4.0

func (c *Config) WithDialOptions(opts *websocket.DialOptions) *Config

func (*Config) WithHeartbeat added in v0.4.0

func (c *Config) WithHeartbeat(heartbeat time.Duration) *Config

func (*Config) WithListening added in v0.4.0

func (c *Config) WithListening(listening bool) *Config

func (*Config) WithLogger added in v0.5.0

func (c *Config) WithLogger(logger golog.Logger) *Config

func (*Config) WithOnClose added in v0.4.0

func (c *Config) WithOnClose(callback func()) *Config

func (*Config) WithOnConnect added in v0.4.0

func (c *Config) WithOnConnect(callback func()) *Config

func (*Config) WithOnError added in v0.4.0

func (c *Config) WithOnError(callback func(error)) *Config

func (*Config) WithOnMessage added in v0.4.0

func (c *Config) WithOnMessage(callback func(gows.MessageType, []byte)) *Config

func (*Config) WithReadLimit added in v0.4.0

func (c *Config) WithReadLimit(limit int64) *Config

func (*Config) WithURL added in v0.4.0

func (c *Config) WithURL(url string) *Config

Jump to

Keyboard shortcuts

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