Documentation
¶
Overview ¶
Package websocket provides type-safe WebSocket support with automatic OpenAPI documentation
Index ¶
- Constants
- func CreateUpgrader(config *Config) websocket.Upgrader
- func GenerateConnectionID() string
- func HandleHandler(conn *Connection, handler Handler, config *Config) error
- func HandleTypedHandler(conn *Connection, handler interface{}, config *Config) error
- func SetupConnection(conn *Connection, config *Config)
- func Writer(conn *Connection, config *Config)
- type Config
- type Connection
- func (c *Connection) Close() error
- func (c *Connection) Context() echo.Context
- func (c *Connection) GetMetadata(key string) (interface{}, bool)
- func (c *Connection) IsClosed() bool
- func (c *Connection) ReadJSON(v interface{}) error
- func (c *Connection) RemoteAddr() string
- func (c *Connection) Send(message interface{}) error
- func (c *Connection) SendJSON(v interface{}) error
- func (c *Connection) SendRaw(data []byte) error
- func (c *Connection) SendTyped(msgType string, payload interface{}) error
- func (c *Connection) SetMetadata(key string, value interface{})
- type Handler
- type Hub
- func (h *Hub) Broadcast(message interface{}) error
- func (h *Hub) BroadcastRaw(data []byte)
- func (h *Hub) Count() int
- func (h *Hub) ForEach(fn func(*Connection))
- func (h *Hub) GetConnection(id string) (*Connection, bool)
- func (h *Hub) Register(conn *Connection)
- func (h *Hub) Run()
- func (h *Hub) Stop()
- func (h *Hub) Unregister(conn *Connection)
- type Message
- type Route
Constants ¶
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 ¶
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 ¶
NewConnection creates a new WebSocket connection wrapper
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 (*Hub) BroadcastRaw ¶
BroadcastRaw sends raw bytes to all 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) Unregister ¶
func (h *Hub) Unregister(conn *Connection)
Unregister removes a connection from the hub