Documentation
¶
Overview ¶
Package gateway provides a WebSocket-based gateway for real-time communication.
Index ¶
- type Config
- type Connection
- type ErrorPayload
- type Gateway
- func (g *Gateway) Broadcast(msg *Message)
- func (g *Gateway) BroadcastToUser(userID string, msg *Message)
- func (g *Gateway) Config() Config
- func (g *Gateway) GetConnection(id string) (*Connection, bool)
- func (g *Gateway) GetConnections() []*Connection
- func (g *Gateway) HandleWebSocket(w http.ResponseWriter, r *http.Request)
- func (g *Gateway) Methods() []string
- func (g *Gateway) RegisterHandler(method string, handler RequestHandler)
- func (g *Gateway) Stats() map[string]interface{}
- func (g *Gateway) Stop()
- type Handler
- func (h *Handler) CloseConnection(c echo.Context) error
- func (h *Handler) GetConnection(c echo.Context) error
- func (h *Handler) ListConnections(c echo.Context) error
- func (h *Handler) RegisterRoutes(e *echo.Echo, g *echo.Group)
- func (h *Handler) Status(c echo.Context) error
- func (h *Handler) WebSocket(c echo.Context) error
- type Message
- type MessageType
- type RequestHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Enabled indicates if the gateway is enabled.
Enabled bool `yaml:"enabled"`
// ReadBufferSize is the WebSocket read buffer size.
ReadBufferSize int `yaml:"read_buffer_size"`
// WriteBufferSize is the WebSocket write buffer size.
WriteBufferSize int `yaml:"write_buffer_size"`
// MaxMessageSize is the maximum message size in bytes.
MaxMessageSize int64 `yaml:"max_message_size"`
// PingInterval is the interval for ping messages.
PingIntervalSeconds int `yaml:"ping_interval_seconds"`
// PongTimeout is the timeout for pong responses.
PongTimeoutSeconds int `yaml:"pong_timeout_seconds"`
// WriteTimeout is the timeout for write operations.
WriteTimeoutSeconds int `yaml:"write_timeout_seconds"`
// RequestTimeout is the timeout for a single gateway request.
// 0 disables hard request timeout and relies on connection cancellation.
RequestTimeoutSeconds int `yaml:"request_timeout_seconds"`
// MaxConnections is the maximum number of connections.
MaxConnections int `yaml:"max_connections"`
}
Config contains gateway configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default gateway configuration.
type Connection ¶
type Connection struct {
ID string
UserID string
// Metadata
Metadata map[string]interface{}
// Statistics
MessagesSent atomic.Int64
MessagesReceived atomic.Int64
ConnectedAt time.Time
// contains filtered or unexported fields
}
Connection represents a WebSocket connection.
func NewConnection ¶
NewConnection creates a new connection.
func (*Connection) Info ¶
func (c *Connection) Info() map[string]interface{}
Info returns connection information.
func (*Connection) Send ¶
func (c *Connection) Send(msg *Message) error
Send sends a message to the connection.
func (*Connection) SetUserID ¶
func (c *Connection) SetUserID(userID string)
SetUserID sets the user ID for the connection.
type ErrorPayload ¶
ErrorPayload represents an error in a message.
type Gateway ¶
type Gateway struct {
// contains filtered or unexported fields
}
Gateway manages WebSocket connections.
func NewGateway ¶
NewGateway creates a new gateway.
func (*Gateway) BroadcastToUser ¶
BroadcastToUser sends a message to all connections for a user.
func (*Gateway) GetConnection ¶
func (g *Gateway) GetConnection(id string) (*Connection, bool)
GetConnection returns a connection by ID.
func (*Gateway) GetConnections ¶
func (g *Gateway) GetConnections() []*Connection
GetConnections returns all connections.
func (*Gateway) HandleWebSocket ¶
func (g *Gateway) HandleWebSocket(w http.ResponseWriter, r *http.Request)
HandleWebSocket handles WebSocket upgrade requests.
func (*Gateway) RegisterHandler ¶
func (g *Gateway) RegisterHandler(method string, handler RequestHandler)
RegisterHandler registers a request handler.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides HTTP handlers for gateway management.
func NewHandler ¶
NewHandler creates a new gateway handler.
func NewHandlerWithAuth ¶
NewHandlerWithAuth creates a new gateway handler with JWT authentication.
func (*Handler) CloseConnection ¶
CloseConnection closes a connection. @Summary Close gateway connection @Tags gateway @Param id path string true "Connection ID" @Success 200 {object} map[string]string @Failure 404 {object} map[string]string @Router /api/v1/gateway/connections/{id} [delete]
func (*Handler) GetConnection ¶
GetConnection returns a connection by ID. @Summary Get gateway connection @Tags gateway @Produce json @Param id path string true "Connection ID" @Success 200 {object} map[string]interface{} @Failure 404 {object} map[string]string @Router /api/v1/gateway/connections/{id} [get]
func (*Handler) ListConnections ¶
ListConnections returns all connections. @Summary List gateway connections @Tags gateway @Produce json @Success 200 {array} map[string]interface{} @Router /api/v1/gateway/connections [get]
func (*Handler) RegisterRoutes ¶
RegisterRoutes registers gateway routes.
func (*Handler) Status ¶
Status returns gateway status. @Summary Get gateway status @Tags gateway @Produce json @Success 200 {object} map[string]interface{} @Router /api/v1/gateway/status [get]
type Message ¶
type Message struct {
ID string `json:"id"`
Type MessageType `json:"type"`
Method string `json:"method,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
Error *ErrorPayload `json:"error,omitempty"`
Timestamp int64 `json:"timestamp"`
}
Message represents a gateway message.
type MessageType ¶
type MessageType string
MessageType represents the type of gateway message.
const ( // TypeRequest is a request message. TypeRequest MessageType = "request" // TypeResponse is a response message. TypeResponse MessageType = "response" // TypeEvent is an event message. TypeEvent MessageType = "event" // TypeError is an error message. TypeError MessageType = "error" )
type RequestHandler ¶
RequestHandler handles gateway requests.