ws

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package ws provides low-level WebSocket protocol implementation. This is an internal package not meant for direct public use.

Index

Constants

View Source
const (
	OpcodeContinuation = 0
	OpcodeText         = 1
	OpcodeBinary       = 2
	OpcodeClose        = 8
	OpcodePing         = 9
	OpcodePong         = 10
)

Frame opcodes defined in RFC 6455

View Source
const (
	CloseNormalClosure           = 1000
	CloseGoingAway               = 1001
	CloseProtocolError           = 1002
	CloseUnsupportedData         = 1003
	CloseNoStatusReceived        = 1005
	CloseAbnormalClosure         = 1006
	CloseInvalidFramePayloadData = 1007
	ClosePolicyViolation         = 1008
	CloseMessageTooBig           = 1009
	CloseMandatoryExtension      = 1010
	CloseInternalServerError     = 1011
	CloseServiceRestart          = 1012
	CloseTryAgainLater           = 1013
	CloseTLSHandshake            = 1015
)

Close codes defined in RFC 6455

Variables

View Source
var (
	ErrInvalidFrame           = errors.New("invalid frame")
	ErrControlFrameTooBig     = errors.New("control frame too big")
	ErrFragmentedControl      = errors.New("fragmented control frame")
	ErrUnexpectedContinuation = errors.New("unexpected continuation frame")
	ErrInvalidCloseCode       = errors.New("invalid close code")
	ErrInvalidUTF8            = errors.New("invalid UTF-8 in text frame")
)

Frame errors

View Source
var (
	ErrNotWebSocket       = errors.New("not a websocket handshake")
	ErrBadHandshake       = errors.New("bad handshake")
	ErrUnsupportedVersion = errors.New("unsupported websocket version")
	ErrMissingKey         = errors.New("missing Sec-WebSocket-Key")
)

Errors

View Source
var ErrMessageTooBig = errors.New("message too big")

Error returned when message exceeds size limit

Functions

func PerformHandshake

func PerformHandshake(w http.ResponseWriter, r *http.Request, opts *HandshakeOptions) (net.Conn, *bufio.ReadWriter, error)

PerformHandshake performs the WebSocket handshake

func ValidateHandshake

func ValidateHandshake(r *http.Request) error

ValidateHandshake validates a WebSocket upgrade request

Types

type CloseError

type CloseError struct {
	Code int
	Text string
}

CloseError represents a close frame error

func (*CloseError) Error

func (e *CloseError) Error() string

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn represents a low-level WebSocket connection

func NewConn

func NewConn(netConn net.Conn, buf *bufio.ReadWriter, isServer bool, maxMessageSize int64) *Conn

NewConn creates a new WebSocket connection

func (*Conn) Close

func (c *Conn) Close() error

Close closes the WebSocket connection

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the local network address

func (*Conn) ReadFrame

func (c *Conn) ReadFrame() (*Frame, error)

ReadFrame reads the next frame from the connection

func (*Conn) ReadMessage

func (c *Conn) ReadMessage() (messageType int, data []byte, err error)

ReadMessage reads a complete message (handling fragmentation)

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the write deadline

func (*Conn) WriteControl

func (c *Conn) WriteControl(opcode int, data []byte) error

WriteControl writes a control frame (close, ping, pong)

func (*Conn) WriteFrame

func (c *Conn) WriteFrame(frame *Frame) error

WriteFrame writes a frame to the connection

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data []byte) error

WriteMessage writes a complete message

type Frame

type Frame struct {
	Fin     bool
	RSV1    bool
	RSV2    bool
	RSV3    bool
	Opcode  int
	Masked  bool
	Payload []byte
	MaskKey [4]byte
}

Frame represents a WebSocket frame

func (*Frame) IsControl

func (f *Frame) IsControl() bool

IsControl returns true if this is a control frame

func (*Frame) IsData

func (f *Frame) IsData() bool

IsData returns true if this is a data frame

type FrameReader

type FrameReader struct {
	// contains filtered or unexported fields
}

FrameReader reads WebSocket frames

func NewFrameReader

func NewFrameReader(r *bufio.Reader, maxMessageSize int64) *FrameReader

NewFrameReader creates a new frame reader

func (*FrameReader) ReadFrame

func (fr *FrameReader) ReadFrame() (*Frame, error)

ReadFrame reads a single frame from the reader

type FrameWriter

type FrameWriter struct {
	// contains filtered or unexported fields
}

FrameWriter writes WebSocket frames

func NewFrameWriter

func NewFrameWriter(w *bufio.Writer, isServer bool) *FrameWriter

NewFrameWriter creates a new frame writer

func (*FrameWriter) WriteFrame

func (fw *FrameWriter) WriteFrame(frame *Frame) error

WriteFrame writes a frame to the writer

type HandshakeOptions

type HandshakeOptions struct {
	// CheckOrigin returns true if the request Origin header is acceptable
	CheckOrigin func(r *http.Request) bool
	// Subprotocols is the list of supported subprotocols
	Subprotocols []string
	// Extensions is the list of supported extensions
	Extensions []string
	// BeforeUpgrade is called before the upgrade response is sent
	BeforeUpgrade func(w http.ResponseWriter, r *http.Request) error
}

HandshakeOptions contains options for WebSocket handshake

Jump to

Keyboard shortcuts

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