websocket

package
v2.0.0-...-1ee353d Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Overview

Package websocket is the Engine.IO WebSocket transport, built on gobwas/ws.

The codec (Encode, Decode) handles the contents of one WebSocket data message as an Engine.IO v4 packet; message.go handles RFC 6455 headers, masking, fragmentation, control frames and bounded reads. Engine.IO PING/PONG packets are distinct from WebSocket ping/pong control frames.

permessage-deflate is not negotiated: the handshake never offers or accepts an extension.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPacket indicates malformed data or an unrepresentable packet.
	ErrInvalidPacket = errors.New("websocket: invalid packet")
	// ErrTooLarge indicates message contents exceed the caller's byte limit.
	ErrTooLarge = errors.New("websocket: packet too large")
	// ErrInvalidLimit indicates a nonpositive message size limit.
	ErrInvalidLimit = errors.New("websocket: message size limit must be positive")
)
View Source
var Default = &Transport{}

Default is default transport.

View Source
var ErrMessageType = errors.New("websocket: only text and binary messages are supported")

ErrMessageType is returned by a write of anything but a text or binary message.

View Source
var ErrNotHijacker = errors.New("websocket: response writer does not support hijacking")

ErrNotHijacker is returned by Accept when the response writer does not implement http.Hijacker (HTTP/2, or a wrapper that hides the interface). The server answers HTTP 501.

Functions

func Encode

func Encode(p Packet, supportsBinary bool, maxBytes int) (frame.Type, []byte, error)

Encode returns the WebSocket data message type and contents for p. When supportsBinary is false, binary data uses a text message with a 'b' prefix and canonical padded base64, as in the JS parser. Otherwise binary data is sent unchanged, including empty data. Text data gets a type byte and may contain the polling record separator: WebSocket messages already provide boundaries. maxBytes must be positive and limits encoded contents, excluding WebSocket headers. The result owns its bytes; errors never return partial contents.

Types

type DialError

type DialError struct {
	Response *http.Response
	// contains filtered or unexported fields
}

DialError is the error when dialing to a server. It saves Response from server.

func (DialError) Unwrap

func (e DialError) Unwrap() error

Unwrap returns the dial failure, for errors.Is and errors.As.

type HandshakeError

type HandshakeError struct {
	Err error
}

HandshakeError is the error of a handshake the server rejected. Accept has already answered the request, so the caller must not write to the response.

func (HandshakeError) Error

func (e HandshakeError) Error() string

func (HandshakeError) Unwrap

func (e HandshakeError) Unwrap() error

type Packet

type Packet struct {
	Frame frame.Type
	Type  packet.Type
	Data  []byte
}

Packet contains data without an Engine.IO type prefix. Binary packets must have type MESSAGE; the v4 binary encoding cannot represent any other type.

func Decode

func Decode(messageType frame.Type, body []byte, maxBytes int) (Packet, error)

Decode decodes one complete WebSocket data message. Raw binary messages are MESSAGE packets with no Engine.IO type byte. Text messages beginning with 'b' decode to binary MESSAGE packets, matching the JS parser's base64 fallback. Text otherwise requires a type byte in '0'..'6' and valid UTF-8 data. maxBytes must be positive and counts encoded contents, not WebSocket headers. Callers must also limit reads before buffering; this function cannot bound their allocations. Results own their data; errors return the zero Packet. As in the staged polling codec, base64 must be canonical and contain no whitespace.

type Transport

type Transport struct {
	// ReadBufferSize and WriteBufferSize are I/O buffer sizes in bytes. A zero
	// ReadBufferSize reads the socket unbuffered after the handshake, which keeps
	// no memory per idle connection. A zero WriteBufferSize sizes the buffer of
	// each message to hold it in one frame.
	ReadBufferSize  int
	WriteBufferSize int

	// MaxPayload limits one message, in wire bytes (frames of a fragmented
	// message together), on both sides. A larger message ends the connection with
	// close status 1009. Zero means payload.DefaultMaxPayload.
	MaxPayload int

	Subprotocols     []string
	TLSClientConfig  *tls.Config
	HandshakeTimeout time.Duration

	// Proxy returns the HTTP proxy for a dial, or nil for none. Only http proxy
	// URLs are supported, through CONNECT; any other scheme fails the dial.
	Proxy       func(*http.Request) (*url.URL, error)
	NetDial     func(network, addr string) (net.Conn, error)
	CheckOrigin func(r *http.Request) bool
}

Transport is websocket transport. The upgrade hijacks an HTTP/1.1 connection, and permessage-deflate is never negotiated.

func (*Transport) Accept

Accept accepts a http request and create Conn.

func (*Transport) Dial

func (t *Transport) Dial(u *url.URL, requestHeader http.Header) (transport.Conn, error)

Dial creates a new client connection.

func (*Transport) Name

func (t *Transport) Name() string

Name is the name of websocket transport.

Jump to

Keyboard shortcuts

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