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 ¶
- Variables
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) HandshakeResponse() *http.Response
- func (c *Client) IsListening() bool
- func (c *Client) On(eventType gows.EventType, callback any) error
- func (c *Client) Read(ctx context.Context, v any) (gows.MessageType, []byte, error)
- func (c *Client) Reader(ctx context.Context) (gows.MessageType, io.Reader, error)
- func (c *Client) StartListening() error
- func (c *Client) StopListening()
- func (c *Client) Write(ctx context.Context, typ gows.MessageType, p []byte) error
- func (c *Client) Writer(ctx context.Context, typ gows.MessageType) (io.WriteCloser, error)
- type Config
- func (c *Config) Clone() *Config
- func (c *Config) WithContext(ctx context.Context) *Config
- func (c *Config) WithDialOptions(opts *websocket.DialOptions) *Config
- func (c *Config) WithHeartbeat(heartbeat time.Duration) *Config
- func (c *Config) WithListening(listening bool) *Config
- func (c *Config) WithLogger(logger golog.Logger) *Config
- func (c *Config) WithOnClose(callback func()) *Config
- func (c *Config) WithOnConnect(callback func()) *Config
- func (c *Config) WithOnError(callback func(error)) *Config
- func (c *Config) WithOnMessage(callback func(gows.MessageType, []byte)) *Config
- func (c *Config) WithReadLimit(limit int64) *Config
- func (c *Config) WithURL(url string) *Config
Constants ¶
This section is empty.
Variables ¶
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 (*Client) Close ¶
Close gracefully closes the WebSocket connection. It's safe to call Close multiple times.
func (*Client) HandshakeResponse ¶
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
IsListening returns whether the client is currently listening for messages.
func (*Client) On ¶ added in v0.3.0
Supported event types: EventConnect, EventClose, EventMessage, EventError.
func (*Client) Read ¶
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 ¶
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
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 ¶
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 (*Config) WithContext ¶ added in v0.4.0
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 (*Config) WithListening ¶ added in v0.4.0
func (*Config) WithLogger ¶ added in v0.5.0
func (*Config) WithOnClose ¶ added in v0.4.0
func (*Config) WithOnConnect ¶ added in v0.4.0
func (*Config) WithOnError ¶ added in v0.4.0
func (*Config) WithOnMessage ¶ added in v0.4.0
func (c *Config) WithOnMessage(callback func(gows.MessageType, []byte)) *Config