coder

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: MIT Imports: 10 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 - 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")

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

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

On registers a callback function for the specified event type. This allows dynamic configuration of event handlers after client creation. Supported event types: EventConnect, EventClose, EventMessage

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

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

Config holds the configuration for the client.

Jump to

Keyboard shortcuts

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