Documentation
¶
Overview ¶
Package uws provides an asynchronous WebSocket server built on UIO.
Index ¶
- Constants
- Variables
- type CloseEvent
- type Conn
- func (c *Conn) Close(code uint16, reason string) error
- 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) Request() *http.Request
- func (c *Conn) SendBinary(payload []byte) error
- func (c *Conn) SendText(payload []byte) error
- func (c *Conn) SetDeadline(deadline time.Time) error
- func (c *Conn) SetNoDelay(noDelay bool) error
- func (c *Conn) SetReadDeadline(deadline time.Time) error
- func (c *Conn) SetUserdata(value any)
- func (c *Conn) SetWriteDeadline(deadline time.Time) error
- func (c *Conn) Subprotocol() string
- func (c *Conn) Userdata() any
- type Dialer
- type Handler
- type Message
- type MessageType
- type Server
Constants ¶
const ( // DefaultMaxHeaderBytes is the default HTTP upgrade header limit. DefaultMaxHeaderBytes = handshake.DefaultMaxBytes // DefaultMaxFramePayload is the default wire payload limit for one frame. DefaultMaxFramePayload = 16 << 20 // DefaultMaxMessageSize is the default decompressed message size limit. DefaultMaxMessageSize = 64 << 20 // DefaultCloseTimeout is the default graceful close handshake timeout. DefaultCloseTimeout = 5 * time.Second // DefaultHandshakeTimeout is the default HTTP upgrade timeout. DefaultHandshakeTimeout = 10 * time.Second )
Variables ¶
var ( // ErrClosed reports an operation on a closed connection. ErrClosed = errors.New("uws: connection closed") // ErrNotReady reports an operation attempted before the handshake completes. ErrNotReady = errors.New("uws: handshake not complete") // ErrBackpressure reports that the UIO connection outbound queue is full. ErrBackpressure = errors.New("uws: outbound queue is full") // ErrServerStarted reports a second attempt to run a Server listener after // its transport has already started. ErrServerStarted = errors.New("uws: server already started") // ErrProtocol reports a WebSocket protocol violation. ErrProtocol = frame.ErrProtocol )
Functions ¶
This section is empty.
Types ¶
type CloseEvent ¶ added in v1.1.0
type CloseEvent struct {
// Code is the peer or locally generated WebSocket close status code.
Code uint16
// Reason is the optional close reason sent with Code.
Reason string
// Err is the transport, protocol, or application error that caused closure.
Err error
}
CloseEvent describes why a WebSocket connection closed.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is an established or handshaking WebSocket connection. UIO serializes protocol and handler callbacks per connection; atomics here coordinate external writers, timers, heartbeat scanning, and transport callbacks. The fields every message touches come first, so they share as few cache lines as the struct's size class allows.
func (*Conn) Close ¶
Close starts a graceful WebSocket close handshake. The Close frame follows every message accepted before it. When the outbound limit has no room for the frame, it waits for the transport to drain instead of failing; either way the connection stops accepting messages, and CloseTimeout bounds the handshake.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the peer transport address.
func (*Conn) Request ¶ added in v1.1.0
Request returns the HTTP upgrade request during OnOpen for server connections, whether accepted by Server.Serve or adopted through Server.ServeHTTP. It returns nil afterward — handlers that need it beyond OnOpen must keep it — and for client connections. The request is read-only.
func (*Conn) SendBinary ¶
SendBinary queues payload as one binary message. The transport flushes accepted data in the connection's current or next I/O task.
func (*Conn) SendText ¶
SendText queues payload as one text message. The transport flushes accepted data in the connection's current or next I/O task. payload must be valid UTF-8: it is sent as given, and only received text is validated.
func (*Conn) SetDeadline ¶ added in v1.1.0
SetDeadline forwards the transport read and write deadline. A zero value clears it.
func (*Conn) SetNoDelay ¶ added in v1.1.0
SetNoDelay controls whether the underlying TCP connection uses Nagle's algorithm.
func (*Conn) SetReadDeadline ¶ added in v1.1.0
SetReadDeadline forwards the transport read deadline. A zero value clears it.
func (*Conn) SetUserdata ¶ added in v1.1.0
SetUserdata replaces the user data associated with the connection. It is not safe for concurrent use from multiple goroutines.
func (*Conn) SetWriteDeadline ¶ added in v1.1.0
SetWriteDeadline forwards the transport write deadline. A zero value clears it.
func (*Conn) Subprotocol ¶
Subprotocol returns the negotiated WebSocket subprotocol, or an empty string.
type Dialer ¶
type Dialer struct {
// Events configures the underlying UIO transport. Nil uses a default Events.
Events *uio.Events
// Subprotocols lists protocols offered in preference order.
Subprotocols []string
// MaxHeaderBytes bounds the HTTP upgrade response header. Zero uses the default.
MaxHeaderBytes int
// MaxFramePayload bounds each wire frame payload. On reads, it also bounds
// aggregate compressed payload across a fragmented message.
MaxFramePayload uint64
// MaxMessageSize bounds a complete message after decompression.
MaxMessageSize uint64
// EnableCompression offers RFC 7692 permessage-deflate.
EnableCompression bool
// CompressionLevel selects the flate level. Zero uses default compression.
CompressionLevel int
// DisableUTF8Check skips Text Message UTF-8 validation. This violates RFC
// 6455 and should only be used with trusted peers. Close reasons remain validated.
DisableUTF8Check bool
// 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
// contains filtered or unexported fields
}
Dialer creates outbound WebSocket connections over a shared UIO transport. Configure its fields before the first Dial; the first Dial freezes the connection configuration.
func NewDialer ¶
func NewDialer() *Dialer
NewDialer returns a Dialer configured with default transport settings.
func (*Dialer) Close ¶
Close permanently stops the dialer, cancels in-flight connection attempts, and requests closure of established connections. Subsequent Dial calls return the first close cause. OnClose reports connection completion.
func (*Dialer) Dial ¶
Dial starts a WebSocket connection. The supplied context bounds the TCP connection and WebSocket handshake; it no longer affects the connection after OnOpen. The returned connection remains pending until Handler.OnOpen reports handshake success or Handler.OnClose reports a handshake failure. The returned error covers setup, network dial, and upgrade-request write or flush failures. A handshake response failure is reported through OnClose.
type Handler ¶
type Handler interface {
// OnOpen is called after a successful WebSocket handshake.
OnOpen(*Conn)
// OnMessage receives complete messages in wire order.
OnMessage(*Conn, Message)
// OnClose is called once after the transport closes. A client handshake
// failure may call it without a preceding OnOpen.
OnClose(*Conn, CloseEvent)
}
Handler receives ordered lifecycle and message callbacks for a connection.
type Message ¶
type Message struct {
// Type identifies whether Payload contains text or binary data.
Type MessageType
// Payload is valid until OnMessage returns. Call Clone before retaining it
// or handing it to another goroutine.
Payload []byte
}
Message is one complete text or binary WebSocket message.
type MessageType ¶
type MessageType uint8
MessageType identifies the payload format of a WebSocket message.
const ( // TextMessage identifies a UTF-8 text message. TextMessage MessageType = 1 // BinaryMessage identifies a binary message. BinaryMessage MessageType = 2 )
type Server ¶
type Server struct {
// Events configures the underlying UIO transport. Nil uses a default Events.
Events *uio.Events
// Handler receives connection lifecycle and message callbacks. It may be nil.
Handler Handler
// CheckOrigin accepts an upgrade request. Nil accepts every origin.
CheckOrigin func(*http.Request) bool
// Subprotocols lists supported protocols in server preference order.
Subprotocols []string
// MaxHeaderBytes bounds HTTP upgrade headers read by the native Serve path.
// When using ServeHTTP, configure http.Server.MaxHeaderBytes instead.
MaxHeaderBytes int
// MaxFramePayload bounds each wire frame payload. On reads, it also bounds
// aggregate compressed payload across a fragmented message.
MaxFramePayload uint64
// MaxMessageSize bounds a complete message after decompression.
MaxMessageSize uint64
// EnableCompression enables RFC 7692 permessage-deflate negotiation.
EnableCompression bool
// CompressionLevel selects the flate level. Zero uses default compression.
CompressionLevel int
// DisableUTF8Check skips Text Message UTF-8 validation. This violates RFC
// 6455 and should only be used with trusted peers. Close reasons remain validated.
DisableUTF8Check bool
// 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
// 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 enables server pings when positive.
HeartbeatInterval time.Duration
// HeartbeatTimeout bounds both the time a ping may remain queued and the
// time to receive its matching pong after it is written. Zero uses twice
// the interval.
HeartbeatTimeout time.Duration
// contains filtered or unexported fields
}
Server accepts WebSocket connections over a UIO transport. Configure its fields before calling Serve; Serve freezes the connection configuration.
func (*Server) Close ¶
Close permanently stops the server and requests transport shutdown. Serve returns after all event loops and connection tasks have exited.
func (*Server) Serve ¶
Serve starts the event loops and listens on each supplied address. Calling Serve without an address prepares the Server for use as an http.Handler.
func (*Server) ServeHTTP ¶ added in v1.1.0
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP upgrades a plain HTTP/1.1 TCP connection and transfers it from net/http to the Server's UIO event loops. Serve must already be running. TLS and HTTP/2 are not supported.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
autobahn
|
|
|
server
command
|
|
|
examples
|
|
|
echo
command
|
|
|
nethttp
command
|
|
|
internal
|
|
|
compress
Package compress implements RFC 7692 message DEFLATE with bounded output and negotiated context takeover.
|
Package compress implements RFC 7692 message DEFLATE with bounded output and negotiated context takeover. |
|
extension
Package extension parses and negotiates WebSocket extension headers.
|
Package extension parses and negotiates WebSocket extension headers. |
|
frame
Package frame implements the wire-level WebSocket frame codec.
|
Package frame implements the wire-level WebSocket frame codec. |
|
handshake
Package handshake parses, validates, and builds RFC 6455 HTTP upgrades.
|
Package handshake parses, validates, and builds RFC 6455 HTTP upgrades. |