gows

package module
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: 1 Imported by: 0

README

Gows

Simplified WebSocket Wrapper for Go.

License

MIT

Acknowledgments

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Connect establishes a WebSocket connection to the server.
	// It takes a context for cancellation and timeout control.
	// Returns an error if the connection fails.
	Connect(ctx context.Context) error
	// Close gracefully closes the WebSocket connection.
	// It should be idempotent.
	// Returns an error if closing the connection fails.
	Close() error

	// Read reads a message from the WebSocket connection.
	// It blocks until a message is received, the context is cancelled, or an error occurs.
	// The `v` parameter is an interface to allow unmarshalling into different types (e.g., a struct for JSON).
	// It returns the MessageType (Text or Binary), the raw message payload as a byte slice, and an error.
	// Returns io.EOF if the connection is closed by the peer.
	Read(ctx context.Context, v any) (MessageType, []byte, error)
	// Write sends a message to the WebSocket connection.
	// It blocks until the message is sent, the context is cancelled, or an error occurs.
	// `typ` specifies the MessageType (Text or Binary) of the message.
	// `p` is the byte slice containing the message payload.
	// Returns an error if the write operation fails or the connection is closed.
	Write(ctx context.Context, typ MessageType, p []byte) 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, EventError
	On(eventType EventType, callback any) error
}

type EventType added in v0.3.0

type EventType int

EventType represents the type of event for callback registration.

const (
	// EventConnect is triggered when the WebSocket connection is established.
	EventConnect EventType = iota
	// EventClose is triggered when the WebSocket connection is closed.
	EventClose
	// EventMessage is triggered when a message is received.
	EventMessage
	// EventError is triggered when an error occurs.
	EventError
)

type MessageType

type MessageType int

MessageType represents the type of message for callback registration.

const (
	// MessageText is for UTF-8 encoded text messages like JSON.
	MessageText MessageType = iota + 1
	// MessageBinary is for binary messages like protobufs.
	MessageBinary
)

Directories

Path Synopsis
Package coder provides a configurable WebSocket client.
Package coder provides a configurable WebSocket client.
example
coder/auto command
coder/base command
coder/on command
Example demonstrating the use of On method for dynamic callback configuration
Example demonstrating the use of On method for dynamic callback configuration

Jump to

Keyboard shortcuts

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