ws

package module
v2.0.0-...-2247a3d Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 26 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(opts ...ClientOption) network.Client

NewClient returns a new client.

Types

type CheckOriginFunc

type CheckOriginFunc func(r *http.Request) bool

type ClientOption

type ClientOption func(o *clientOptions)

func WithClientCloseTimeout

func WithClientCloseTimeout(closeTimeout time.Duration) ClientOption

WithClientCloseTimeout sets the graceful close timeout.

When the write queue has not drained before the timeout elapses, the wait is abandoned and the connection is closed forcibly. The default value of 0 means no limit.

func WithClientCompression

func WithClientCompression(enableCompression bool) ClientOption

WithClientCompression sets whether to enable compression.

func WithClientCompressionLevel

func WithClientCompressionLevel(compressionLevel int) ClientOption

WithClientCompressionLevel sets the compression level.

func WithClientCredentials

func WithClientCredentials(caFile string, serverName string) ClientOption

WithClientCredentials sets the CA certificate and the server name to verify.

func WithClientDialTimeout

func WithClientDialTimeout(dialTimeout time.Duration) ClientOption

WithClientDialTimeout sets the dial timeout.

func WithClientHeartbeatInterval

func WithClientHeartbeatInterval(heartbeatInterval time.Duration) ClientOption

WithClientHeartbeatInterval sets the heartbeat interval.

func WithClientReadBufferSize

func WithClientReadBufferSize(readBufferSize int) ClientOption

WithClientReadBufferSize sets the read buffer size.

func WithClientTLSConfig

func WithClientTLSConfig(tlsConfig *tls.Config) ClientOption

WithClientTLSConfig sets the TLS config.

func WithClientUrl

func WithClientUrl(url string) ClientOption

WithClientUrl sets the dial URL.

func WithClientWriteQueueSize

func WithClientWriteQueueSize(writeQueueSize int) ClientOption

WithClientWriteQueueSize sets the write queue size.

func WithClientWriteTimeout

func WithClientWriteTimeout(writeTimeout time.Duration) ClientOption

WithClientWriteTimeout sets the write timeout.

type HeartbeatMechanism

type HeartbeatMechanism string
const (
	RespHeartbeat HeartbeatMechanism = "resp" // Responsive heartbeat
	TickHeartbeat HeartbeatMechanism = "tick" // Active tick heartbeat
)

type ProxyMode

type ProxyMode int
const (
	ProxyModeNone        ProxyMode = iota // No proxy mode
	ProxyModeTransport                    // Transport mode (layer 4 proxy, the server enables the proxy protocol)
	ProxyModeApplication                  // Application mode (layer 7 proxy)
)

type ProxyOptions

type ProxyOptions struct {
	ProxyHeader string            `json:"proxyHeader"` // Client IP header, defaults to "X-Forwarded-For"
	PortHeader  string            `json:"portHeader"`  // Client port header, defaults to "X-Forwarded-Port"
	TrustProxy  TrustProxyOptions `json:"trustProxy"`  // Trust proxy config
}

type Server

type Server interface {
	network.Server
	// OnUpgrade registers the handler invoked when an HTTP request is upgraded.
	OnUpgrade(handler UpgradeHandler)
}

Server is the WebSocket server interface.

func NewServer

func NewServer(opts ...ServerOption) Server

NewServer returns a new server.

type ServerOption

type ServerOption func(o *serverOptions)

func WithServerAddr

func WithServerAddr(addr string) ServerOption

WithServerAddr sets the listen address.

func WithServerAuthorizeTimeout

func WithServerAuthorizeTimeout(authorizeTimeout time.Duration) ServerOption

WithServerAuthorizeTimeout sets the authorization timeout.

func WithServerCheckOrigin

func WithServerCheckOrigin(checkOrigin CheckOriginFunc) ServerOption

WithServerCheckOrigin sets the WebSocket origin check function.

func WithServerCloseTimeout

func WithServerCloseTimeout(closeTimeout time.Duration) ServerOption

WithServerCloseTimeout sets the graceful close timeout.

When the write queue has not drained before the timeout elapses, the wait is abandoned and the connection is closed forcibly. The default value of 0 means no limit.

func WithServerCompressionLevel

func WithServerCompressionLevel(compressionLevel int) ServerOption

WithServerCompressionLevel sets the compression level.

func WithServerCredentials

func WithServerCredentials(certFile, keyFile string) ServerOption

WithServerCredentials sets the server certificate and key.

func WithServerEnableCompression

func WithServerEnableCompression(enableCompression bool) ServerOption

WithServerEnableCompression sets whether to enable compression.

func WithServerHeartbeatInterval

func WithServerHeartbeatInterval(heartbeatInterval time.Duration) ServerOption

WithServerHeartbeatInterval sets the heartbeat check interval.

func WithServerHeartbeatMechanism

func WithServerHeartbeatMechanism(heartbeatMechanism HeartbeatMechanism) ServerOption

WithServerHeartbeatMechanism sets the heartbeat mechanism.

func WithServerMaxConnNum

func WithServerMaxConnNum(maxConnNum int) ServerOption

WithServerMaxConnNum sets the maximum number of connections.

func WithServerPath

func WithServerPath(path string) ServerOption

WithServerPath sets the WebSocket connection path.

func WithServerProxyMode

func WithServerProxyMode(proxyMode ProxyMode) ServerOption

WithServerProxyMode sets the proxy mode.

func WithServerProxyOptions

func WithServerProxyOptions(proxyOpts ProxyOptions) ServerOption

WithServerProxyOptions sets the proxy options.

func WithServerReadBufferSize

func WithServerReadBufferSize(readBufferSize int) ServerOption

WithServerReadBufferSize sets the read buffer size.

func WithServerWriteBufferSize

func WithServerWriteBufferSize(writeBufferSize int) ServerOption

WithServerWriteBufferSize sets the write buffer size.

func WithServerWriteQueueSize

func WithServerWriteQueueSize(writeQueueSize int) ServerOption

WithServerWriteQueueSize sets the write queue size.

func WithServerWriteTimeout

func WithServerWriteTimeout(writeTimeout time.Duration) ServerOption

WithServerWriteTimeout sets the write timeout.

type TrustProxyOptions

type TrustProxyOptions struct {
	Enable    bool     `json:"enable"`    // Whether to trust proxies, defaults to false
	Proxies   []string `json:"proxies"`   // List of trusted proxy IP addresses or CIDR ranges
	LinkLocal bool     `json:"linkLocal"` // Whether to trust all link-local IP ranges (e.g. 169.254.0.0/16, fe80::/10)
	Loopback  bool     `json:"loopback"`  // Whether to trust all loopback IP ranges (e.g. 127.0.0.0/8, ::1/128)
	Private   bool     `json:"private"`   // Whether to trust all private IP ranges (e.g. 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7)
	// contains filtered or unexported fields
}

type UpgradeHandler

type UpgradeHandler func(w http.ResponseWriter, r *http.Request) (allowed bool)

UpgradeHandler is the handler invoked when an HTTP request is upgraded to the WebSocket protocol.

It reports whether the upgrade is allowed.

Jump to

Keyboard shortcuts

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