coder

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 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 (
	ErrNotConnected     = errors.New("websocket client is not connected")
	ErrAlreadyConnected = errors.New("websocket client is already connected")
)

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) 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) 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
}

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