Documentation
¶
Overview ¶
Package codec encodes and decodes WebSocket frames using byte slices. Payload masking is explicit; message assembly and connection handling belong to the caller.
Index ¶
- Constants
- Variables
- func Mask(payload []byte, key [4]byte, offset uint8) uint8
- type Decoder
- type Encoder
- type Header
- func (h *Header) Bytes() []byte
- func (h *Header) Final() bool
- func (h *Header) Len() int
- func (h *Header) MaskKey() []byte
- func (h *Header) Masked() bool
- func (h *Header) Opcode() Opcode
- func (h *Header) PayloadLen() uint64
- func (h *Header) RSV1() bool
- func (h *Header) RSV2() bool
- func (h *Header) RSV3() bool
- type Opcode
Constants ¶
const MaxHeaderSize = 14
MaxHeaderSize is the maximum encoded header size.
Variables ¶
var ( // ErrPayloadPending means NextHeader was called before draining the payload. ErrPayloadPending = errors.New("ews/codec: previous payload has not been consumed") // ErrInvalidPayloadLength indicates a nonminimal or out-of-range length. ErrInvalidPayloadLength = errors.New("ews/codec: invalid payload length encoding") )
var ( // ErrInvalidOpcode is returned for an opcode RFC 6455 does not define. ErrInvalidOpcode = errors.New("ews/codec: invalid opcode") // ErrInvalidControlFrame is returned for a fragmented control frame, a // control payload over 125 bytes, or a one-byte close payload. ErrInvalidControlFrame = errors.New("ews/codec: invalid control frame") // ErrInvalidCompressedFrame is returned by EncodeCompressed for a control // opcode. ErrInvalidCompressedFrame = errors.New("ews/codec: only data frames may be compressed") )
Functions ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder incrementally decodes WebSocket frames. The zero value is ready to use.
func (*Decoder) NextHeader ¶
NextHeader reads a header; ok is false if incomplete. Errors consume nothing. Unread payloads and invalid lengths are errors; other validation is up to the caller.
func (*Decoder) Payload ¶
Payload consumes available bytes from this frame without unmasking. Bytes are borrowed until the next decoder call. Returns nil, false if waiting for input, or nil, true if no payload remains (including before the first header).
func (*Decoder) PayloadN ¶
PayloadN is like Payload but consumes at most n bytes. If n <= 0, it consumes nothing.
type Encoder ¶
type Encoder struct {
// ScratchKeep bounds the masking scratch kept between frames. A payload
// that needs more gets a scratch of its size, which the next frame that
// fits within the bound releases, so one huge masked message does not
// pin a copy for the life of the encoder. Zero keeps whatever was needed.
ScratchKeep int
// contains filtered or unexported fields
}
Encoder prepares WebSocket frames. The zero value is ready to use.
func (*Encoder) Encode ¶
Encode borrows payload if key is nil; otherwise it masks a copy in scratch. Errors leave the frame unchanged.
func (*Encoder) EncodeCompressed ¶
EncodeCompressed encodes precompressed message data, setting RSV1 on its first frame. Continuation frames leave RSV1 clear. Control frames are rejected.
func (*Encoder) HeaderBytes ¶
HeaderBytes borrows the header until the next Encode or Reset.
func (*Encoder) PayloadBytes ¶
PayloadBytes borrows the payload until the next Encode or Reset.
type Header ¶
type Header struct {
// contains filtered or unexported fields
}
Header stores a WebSocket frame header. Accessors assume valid input.
func (*Header) PayloadLen ¶
PayloadLen returns the payload size in bytes.