websocket

package
v1.4.8 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package websocket provides type-safe WebSocket support with automatic OpenAPI documentation

Index

Constants

View Source
const (
	DefaultReadBufferSize  = 1024
	DefaultWriteBufferSize = 1024
	DefaultPingInterval    = 30 * time.Second
	DefaultPongTimeout     = 60 * time.Second
	DefaultMaxMessageSize  = 512 * 1024 // 512KB
)

WebSocket configuration defaults

Variables

This section is empty.

Functions

func CreateUpgrader

func CreateUpgrader(config *Config) websocket.Upgrader

CreateUpgrader creates a websocket.Upgrader from Config

func GenerateConnectionID

func GenerateConnectionID() string

GenerateConnectionID generates a unique connection ID

func HandleHandler

func HandleHandler(conn *Connection, handler Handler, config *Config) error

HandleHandler handles Handler interface implementations

func HandleTypedHandler

func HandleTypedHandler(conn *Connection, handler interface{}, config *Config) error

HandleTypedHandler handles function-based handlers with typed messages

func SetupConnection

func SetupConnection(conn *Connection, config *Config)

SetupConnection configures a connection with read limits and pong handler

func Writer

func Writer(conn *Connection, config *Config)

Writer handles writing messages to the WebSocket

Types

type Config

type Config struct {
	// ReadBufferSize specifies the I/O buffer size in bytes for reads
	ReadBufferSize int
	// WriteBufferSize specifies the I/O buffer size in bytes for writes
	WriteBufferSize int
	// PingInterval is the interval between ping messages
	PingInterval time.Duration
	// PongTimeout is the timeout for pong response
	PongTimeout time.Duration
	// MaxMessageSize is the maximum message size in bytes
	MaxMessageSize int64
	// CheckOrigin is a function to validate the request origin
	CheckOrigin func(r *http.Request) bool
}

Config configures WebSocket behavior

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default WebSocket configuration

type Connection

type Connection struct {
	// ID is a unique identifier for this connection
	ID string
	// contains filtered or unexported fields
}

Connection wraps a WebSocket connection with type-safe operations

func NewConnection

func NewConnection(id string, ws *websocket.Conn, ctx echo.Context) *Connection

NewConnection creates a new WebSocket connection wrapper

func (*Connection) Close

func (c *Connection) Close() error

Close closes the WebSocket connection

func (*Connection) Context

func (c *Connection) Context() echo.Context

Context returns the Echo context

func (*Connection) GetMetadata

func (c *Connection) GetMetadata(key string) (interface{}, bool)

GetMetadata gets a metadata value

func (*Connection) IsClosed

func (c *Connection) IsClosed() bool

IsClosed returns whether the connection is closed

func (*Connection) ReadJSON

func (c *Connection) ReadJSON(v interface{}) error

ReadJSON reads and decodes a JSON message

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() string

RemoteAddr returns the remote address

func (*Connection) Send

func (c *Connection) Send(message interface{}) error

Send sends a message to the client

func (*Connection) SendJSON

func (c *Connection) SendJSON(v interface{}) error

SendJSON sends a JSON-encoded message

func (*Connection) SendRaw

func (c *Connection) SendRaw(data []byte) error

SendRaw sends raw bytes to the client

func (*Connection) SendTyped

func (c *Connection) SendTyped(msgType string, payload interface{}) error

SendTyped sends a typed message with metadata

func (*Connection) SetMetadata

func (c *Connection) SetMetadata(key string, value interface{})

SetMetadata sets a metadata value

type Handler

type Handler interface {
	// OnConnect is called when a client connects
	OnConnect(conn *Connection) error
	// OnMessage is called when a message is received
	OnMessage(conn *Connection, messageType int, data []byte) error
	// OnDisconnect is called when a client disconnects
	OnDisconnect(conn *Connection, err error)
}

Handler is the interface for WebSocket handlers with lifecycle methods

type Hub

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

Hub manages multiple WebSocket connections

func NewHub

func NewHub() *Hub

NewHub creates a new WebSocket hub

func (*Hub) Broadcast

func (h *Hub) Broadcast(message interface{}) error

Broadcast sends a message to all connected clients

func (*Hub) BroadcastRaw

func (h *Hub) BroadcastRaw(data []byte)

BroadcastRaw sends raw bytes to all connected clients

func (*Hub) Count

func (h *Hub) Count() int

Count returns the number of connected clients

func (*Hub) ForEach

func (h *Hub) ForEach(fn func(*Connection))

ForEach iterates over all connections

func (*Hub) GetConnection

func (h *Hub) GetConnection(id string) (*Connection, bool)

GetConnection returns a connection by ID

func (*Hub) Register

func (h *Hub) Register(conn *Connection)

Register adds a connection to the hub

func (*Hub) Run

func (h *Hub) Run()

Run starts the hub's main loop

func (*Hub) Stop

func (h *Hub) Stop()

Stop stops the hub

func (*Hub) Unregister

func (h *Hub) Unregister(conn *Connection)

Unregister removes a connection from the hub

type Message

type Message[T any] struct {
	Type      string    `json:"type"`
	Payload   T         `json:"payload"`
	Timestamp time.Time `json:"timestamp"`
	ID        string    `json:"id,omitempty"`
}

Message wraps messages with metadata

type Route

type Route struct {
	Summary     string
	Description string
	Tags        []string
	Protocols   []string // Sub-protocols supported
}

Route configures WebSocket route metadata for OpenAPI documentation

Jump to

Keyboard shortcuts

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