Documentation
¶
Overview ¶
Package ws provides low-level WebSocket protocol implementation. This is an internal package not meant for direct public use.
Index ¶
- Constants
- Variables
- func PerformHandshake(w http.ResponseWriter, r *http.Request, opts *HandshakeOptions) (net.Conn, *bufio.ReadWriter, error)
- func ValidateHandshake(r *http.Request) error
- type CloseError
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) ReadFrame() (*Frame, error)
- func (c *Conn) ReadMessage() (messageType int, data []byte, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) WriteControl(opcode int, data []byte) error
- func (c *Conn) WriteFrame(frame *Frame) error
- func (c *Conn) WriteMessage(messageType int, data []byte) error
- type Frame
- type FrameReader
- type FrameWriter
- type HandshakeOptions
Constants ¶
const ( OpcodeContinuation = 0 OpcodeText = 1 OpcodeBinary = 2 OpcodeClose = 8 OpcodePing = 9 OpcodePong = 10 )
Frame opcodes defined in RFC 6455
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 ¶
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
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
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 ¶
ValidateHandshake validates a WebSocket upgrade request
Types ¶
type CloseError ¶
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 (*Conn) ReadMessage ¶
ReadMessage reads a complete message (handling fragmentation)
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address
func (*Conn) SetDeadline ¶
SetDeadline sets the read and write deadlines
func (*Conn) SetReadDeadline ¶
SetReadDeadline sets the read deadline
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline
func (*Conn) WriteControl ¶
WriteControl writes a control frame (close, ping, pong)
func (*Conn) WriteFrame ¶
WriteFrame writes a frame to the connection
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
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