Documentation
¶
Index ¶
- Constants
- Variables
- func CheckOriginWithAllowedList(allowedOrigins []string) func(r *http.Request) bool
- func DefaultCheckOrigin(r *http.Request) bool
- func IsCloseError(err error, codes ...int) bool
- func IsUnexpectedCloseError(err error, expectedCodes ...int) bool
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) CloseHandler() func(code int, text string) error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) PingHandler() func(appData string) error
- func (c *Conn) PongHandler() func(appData string) error
- func (c *Conn) ReadJSON(v interface{}) error
- func (c *Conn) ReadMessage() (messageType int, p []byte, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetCloseHandler(h func(code int, text string) error)
- func (c *Conn) SetPingHandler(h func(appData string) error)
- func (c *Conn) SetPongHandler(h func(appData string) error)
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) error
- func (c *Conn) WriteJSON(v interface{}) error
- func (c *Conn) WriteMessage(messageType int, data []byte) error
- type PoolConfig
- type PoolStats
- type Upgrader
- type WebSocketPool
- func (p *WebSocketPool) Close(conn *Conn, reason error) error
- func (p *WebSocketPool) Get(ctx context.Context, endpoint string, upgrader *Upgrader, ...) (*Conn, error)
- func (p *WebSocketPool) GetStats() PoolStats
- func (p *WebSocketPool) Put(conn *Conn) error
- func (p *WebSocketPool) Shutdown(ctx context.Context) error
Constants ¶
const ( TextMessage = ws.OpcodeText BinaryMessage = ws.OpcodeBinary CloseMessage = ws.OpcodeClose PingMessage = ws.OpcodePing PongMessage = ws.OpcodePong )
WebSocket message types
const ( CloseNormalClosure = ws.CloseNormalClosure CloseGoingAway = ws.CloseGoingAway CloseProtocolError = ws.CloseProtocolError CloseUnsupportedData = ws.CloseUnsupportedData CloseNoStatusReceived = ws.CloseNoStatusReceived CloseAbnormalClosure = ws.CloseAbnormalClosure CloseInvalidFramePayloadData = ws.CloseInvalidFramePayloadData ClosePolicyViolation = ws.ClosePolicyViolation CloseMessageTooBig = ws.CloseMessageTooBig CloseMandatoryExtension = ws.CloseMandatoryExtension CloseInternalServerError = ws.CloseInternalServerError CloseServiceRestart = ws.CloseServiceRestart CloseTryAgainLater = ws.CloseTryAgainLater CloseTLSHandshake = ws.CloseTLSHandshake )
WebSocket close codes
Variables ¶
var ( ErrNotWebSocket = ws.ErrNotWebSocket ErrBadHandshake = ws.ErrBadHandshake )
WebSocket errors
Functions ¶
func CheckOriginWithAllowedList ¶
CheckOriginWithAllowedList checks if the origin is in the allowed list
func DefaultCheckOrigin ¶
DefaultCheckOrigin provides a safe default origin check that enforces same-origin policy
func IsCloseError ¶
IsCloseError returns true if the error is a close error with one of the specified codes
func IsUnexpectedCloseError ¶
IsUnexpectedCloseError checks if the error is an unexpected close error
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a WebSocket connection
func (*Conn) CloseHandler ¶
CloseHandler returns the current close handler
func (*Conn) PingHandler ¶
PingHandler returns the current ping handler
func (*Conn) PongHandler ¶
PongHandler returns the current pong handler
func (*Conn) ReadMessage ¶
ReadMessage reads a message from the WebSocket connection
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address
func (*Conn) SetCloseHandler ¶
SetCloseHandler sets the handler for close messages
func (*Conn) SetPingHandler ¶
SetPingHandler sets the handler for ping messages
func (*Conn) SetPongHandler ¶
SetPongHandler sets the handler for pong messages
func (*Conn) SetReadDeadline ¶
SetReadDeadline sets the read deadline on the connection
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline on the connection
func (*Conn) WriteControl ¶
WriteControl writes a control message with the given deadline
type PoolConfig ¶
type PoolConfig struct {
// MaxConnectionsPerEndpoint is the maximum number of connections per endpoint
MaxConnectionsPerEndpoint int
// MaxIdleConnections is the maximum number of idle connections per endpoint
MaxIdleConnections int
// IdleTimeout is how long a connection can be idle before being closed
IdleTimeout time.Duration
// HealthCheckInterval is how often to ping connections to check health
HealthCheckInterval time.Duration
// ConnectionTimeout is the timeout for establishing new connections
ConnectionTimeout time.Duration
// EnableCompression enables WebSocket compression
EnableCompression bool
// OnConnectionCreated is called when a new connection is created
OnConnectionCreated func(endpoint string, conn *Conn)
// OnConnectionClosed is called when a connection is closed
OnConnectionClosed func(endpoint string, conn *Conn, reason error)
}
PoolConfig configures the WebSocket connection pool
func DefaultPoolConfig ¶
func DefaultPoolConfig() PoolConfig
DefaultPoolConfig returns a default pool configuration
type PoolStats ¶
type PoolStats struct {
TotalConnections atomic.Int64
ActiveConnections atomic.Int64
IdleConnections atomic.Int64
FailedConnections atomic.Int64
ConnectionsCreated atomic.Int64
ConnectionsReused atomic.Int64
HealthChecksFailed atomic.Int64
}
PoolStats tracks pool statistics
type Upgrader ¶
type Upgrader struct {
// CheckOrigin returns true if the request Origin header is acceptable
// If nil, a safe default is used that checks for same-origin requests
CheckOrigin func(r *http.Request) bool
// Subprotocols specifies the server's supported protocols in order of preference
Subprotocols []string
// Error specifies the function for generating HTTP error responses
Error func(w http.ResponseWriter, r *http.Request, status int, reason error)
// MaxMessageSize is the maximum size for a message read from the peer
MaxMessageSize int64
// WriteBufferSize is the size of the write buffer
WriteBufferSize int
// ReadBufferSize is the size of the read buffer
ReadBufferSize int
// HandshakeTimeout specifies the duration for the handshake to complete
HandshakeTimeout time.Duration
// EnableCompression specifies if the server should attempt to negotiate compression
EnableCompression bool
// BeforeUpgrade is called after origin check but before sending upgrade response
// This can be used for authentication, rate limiting, or other pre-upgrade checks
BeforeUpgrade func(w http.ResponseWriter, r *http.Request) error
// AllowedOrigins is a list of allowed origins for CORS
// If empty and CheckOrigin is nil, same-origin policy is enforced
AllowedOrigins []string
// RequireProtocol ensures the client specifies one of the supported subprotocols
RequireProtocol bool
}
Upgrader upgrades HTTP connections to WebSocket connections
type WebSocketPool ¶
type WebSocketPool struct {
// contains filtered or unexported fields
}
WebSocketPool manages a pool of WebSocket connections
func NewWebSocketPool ¶
func NewWebSocketPool(config PoolConfig) *WebSocketPool
NewWebSocketPool creates a new WebSocket connection pool
func (*WebSocketPool) Close ¶
func (p *WebSocketPool) Close(conn *Conn, reason error) error
Close closes a connection and removes it from the pool
func (*WebSocketPool) Get ¶
func (p *WebSocketPool) Get(ctx context.Context, endpoint string, upgrader *Upgrader, w http.ResponseWriter, r *http.Request) (*Conn, error)
Get retrieves a connection from the pool or creates a new one
func (*WebSocketPool) GetStats ¶
func (p *WebSocketPool) GetStats() PoolStats
GetStats returns current pool statistics
func (*WebSocketPool) Put ¶
func (p *WebSocketPool) Put(conn *Conn) error
Put returns a connection to the pool