Documentation
¶
Overview ¶
Package uws provides an asynchronous WebSocket server built on UIO.
Index ¶
- Constants
- Variables
- type CloseInfo
- type Conn
- func (c *Conn) BeginMessage(typ MessageType) (*Writer, error)
- func (c *Conn) Close(code uint16, reason string) error
- func (c *Conn) ConnContext() context.Context
- func (c *Conn) Context() any
- func (c *Conn) IsClosed() bool
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Ping(payload []byte) error
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SendBinary(payload []byte) error
- func (c *Conn) SendText(payload []byte) error
- func (c *Conn) SetContext(value any)
- func (c *Conn) Subprotocol() string
- type Dialer
- type Executor
- type Handler
- type Message
- type MessageType
- type Server
- type Writer
Constants ¶
const ( DefaultMaxHeaderBytes = handshake.DefaultMaxBytes DefaultMaxFramePayload = 16 << 20 DefaultMaxMessageSize = 64 << 20 // Leave room for the largest legal WebSocket frame header (including a // client mask) so a maximum-sized default frame is not rejected by the // outbound budget before it reaches the transport. DefaultMaxOutboundBytes = DefaultMaxFramePayload + 14 DefaultCloseTimeout = 5 * time.Second DefaultHandshakeTimeout = 10 * time.Second DefaultSlowClientTimeout = 30 * time.Second DefaultMaxPendingMessages = 64 DefaultMaxPendingBytes = 1 << 20 DefaultMaxPendingTotalMessages = 4096 DefaultMaxPendingTotalBytes = 64 << 20 )
Variables ¶
var ( ErrClosed = errors.New("uws: connection closed") ErrNotReady = errors.New("uws: handshake not complete") ErrBackpressure = errors.New("uws: outbound queue is full") ErrApplicationBackpressure = errors.New("uws: application queue is full") ErrExecutorRejected = errors.New("uws: executor rejected callback") ErrWriterClosed = errors.New("uws: message writer is closed") ErrProtocol = frame.ErrProtocol )
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func (*Conn) BeginMessage ¶
func (c *Conn) BeginMessage(typ MessageType) (*Writer, error)
BeginMessage starts a fragmented message and holds the connection's write ownership until Writer.Close returns. It is non-blocking with respect to the socket; callers must always close the returned writer. With compression enabled, writes are compressed incrementally and emitted as continuation frames, so large messages do not need a second full plaintext buffer.
func (*Conn) ConnContext ¶ added in v1.0.1
ConnContext returns the context scoped to this connection. It is canceled when the connection closes. Client contexts retain values from Dial's context; server contexts derive from Server.BaseContext and Server.ConnContext.
func (*Conn) RemoteAddr ¶
func (*Conn) SendBinary ¶
func (*Conn) SetContext ¶
SetContext replaces the user data associated with the connection.
func (*Conn) Subprotocol ¶
type Dialer ¶
type Dialer struct {
Events *uio.Events
Subprotocols []string
MaxHeaderBytes int
MaxFramePayload uint64
MaxMessageSize uint64
MaxOutboundBytes int
EnableCompression bool
CompressionLevel int
// CloseTimeout bounds the graceful WebSocket close handshake. A zero value
// uses DefaultCloseTimeout.
CloseTimeout time.Duration
// HandshakeTimeout bounds the HTTP upgrade handshake. A zero value uses
// DefaultHandshakeTimeout.
HandshakeTimeout time.Duration
// SlowClientTimeout closes FullDuplex connections that remain blocked on
// socket writes. A zero value uses DefaultSlowClientTimeout.
SlowClientTimeout time.Duration
// Executor dispatches OnOpen, OnMessage, and OnClose callbacks away from
// the I/O event loop. A nil executor preserves synchronous callbacks.
Executor Executor
// MaxPendingMessages bounds queued application messages per connection when
// Executor is enabled. Zero uses DefaultMaxPendingMessages.
MaxPendingMessages int
// MaxPendingBytes bounds queued application payload bytes per connection
// when Executor is enabled. Zero uses DefaultMaxPendingBytes.
MaxPendingBytes int
// MaxPendingTotalMessages bounds queued application messages across all
// connections using this dialer. Zero uses DefaultMaxPendingTotalMessages.
MaxPendingTotalMessages int
// MaxPendingTotalBytes bounds queued application payload bytes across all
// connections using this dialer. Zero uses DefaultMaxPendingTotalBytes.
MaxPendingTotalBytes int
// contains filtered or unexported fields
}
func (*Dialer) Close ¶
Close permanently stops the dialer, cancels in-flight connection attempts, and closes established connections. Subsequent Dial calls return the first close cause.
func (*Dialer) Dial ¶
Dial starts a WebSocket connection. The supplied context bounds the network dial and WebSocket handshake. After a successful handshake, the connection has an independent context that retains its values and is canceled when the connection closes. The returned connection remains pending until Handler.OnOpen reports handshake success or Handler.OnClose reports a handshake failure. The returned error only covers failures that prevent the connection attempt from starting.
type Executor ¶
type Executor interface {
Submit(func()) bool
}
Executor runs application callbacks outside the I/O event loop. Submit must return promptly and reports false when a bounded worker queue is full.
type Handler ¶
type Handler interface {
// OnOpen is called after a successful WebSocket handshake. OnClose is
// called exactly once after the transport closes; a client handshake
// failure calls OnClose without a preceding OnOpen. OnMessage is ordered per
// connection. With Executor enabled, all callbacks for one connection are
// serialized by that executor.
OnOpen(*Conn)
OnMessage(*Conn, Message)
OnClose(*Conn, CloseInfo)
}
type Message ¶
type Message struct {
Type MessageType
// Payload is valid until OnMessage returns. Call Clone before retaining it
// or handing it to another goroutine.
Payload []byte
}
type MessageType ¶
type MessageType uint8
const ( TextMessage MessageType = 1 BinaryMessage MessageType = 2 )
type Server ¶
type Server struct {
Events *uio.Events
Handler Handler
// BaseContext is the parent of the server lifecycle context. A nil value
// uses context.Background. Canceling it closes the server.
BaseContext context.Context
// ConnContext optionally derives an application context for each accepted
// connection before OnOpen. It must return a non-nil context derived from
// the supplied server context and must not block.
ConnContext func(context.Context, *Conn) context.Context
CheckOrigin func(*http.Request) bool
Subprotocols []string
MaxHeaderBytes int
MaxFramePayload uint64
MaxMessageSize uint64
MaxOutboundBytes int
EnableCompression bool
CompressionLevel int
// CloseTimeout bounds the graceful WebSocket close handshake. A zero value
// uses DefaultCloseTimeout.
CloseTimeout time.Duration
// HandshakeTimeout bounds the HTTP upgrade handshake. A zero value uses
// DefaultHandshakeTimeout.
HandshakeTimeout time.Duration
// SlowClientTimeout closes FullDuplex connections that remain blocked on
// socket writes. A zero value uses DefaultSlowClientTimeout.
SlowClientTimeout time.Duration
// Executor dispatches OnOpen, OnMessage, and OnClose callbacks away from
// the I/O event loop. A nil executor preserves synchronous callbacks.
Executor Executor
// MaxPendingMessages bounds queued application messages per connection when
// Executor is enabled. Zero uses DefaultMaxPendingMessages.
MaxPendingMessages int
// MaxPendingBytes bounds queued application payload bytes per connection
// when Executor is enabled. Zero uses DefaultMaxPendingBytes.
MaxPendingBytes int
// MaxPendingTotalMessages bounds queued application messages across all
// connections using this server. Zero uses DefaultMaxPendingTotalMessages.
MaxPendingTotalMessages int
// MaxPendingTotalBytes bounds queued application payload bytes across all
// connections using this server. Zero uses DefaultMaxPendingTotalBytes.
MaxPendingTotalBytes int
// AllowCompressionContextTakeover enables RFC 7692 context takeover for
// peers that do not request a no-context-takeover parameter. It is disabled
// by default to bound per-connection compression state.
AllowCompressionContextTakeover bool
HeartbeatInterval time.Duration
HeartbeatTimeout time.Duration
// contains filtered or unexported fields
}