Documentation
¶
Overview ¶
Package websocket implements a basic websocket server.
Index ¶
- Constants
- Variables
- func MarshalFrame(frame *Frame, mask MaskingKey) []byte
- func WriteFrame(dst io.Writer, mask MaskingKey, frame *Frame) error
- type ClientKey
- type Error
- type Frame
- type Handler
- type Hooks
- type MaskingKey
- type Message
- type Mode
- type Opcode
- type Options
- type RSVBit
- type StatusCode
- type Websocket
Constants ¶
const ( DefaultMaxFrameSize int = 16 << 10 // 16KiB DefaultMaxMessageSize int = 256 << 10 // 256KiB )
Default options.
Variables ¶
var ( ErrClientFrameUnmasked = newError(StatusProtocolError, "client frame must be masked") ErrClosePayloadInvalid = newError(StatusProtocolError, "close frame payload must be at least 2 bytes") ErrCloseStatusInvalid = newError(StatusProtocolError, "close status code out of range") ErrCloseStatusReserved = newError(StatusProtocolError, "close status code is reserved") ErrContinuationExpected = newError(StatusProtocolError, "continuation frame expected") ErrContinuationUnexpected = newError(StatusProtocolError, "continuation frame unexpected") ErrControlFrameFragmented = newError(StatusProtocolError, "control frame must not be fragmented") ErrControlFrameTooLarge = newError(StatusProtocolError, "control frame payload exceeds 125 bytes") ErrInvalidFramePayload = newError(StatusInvalidFramePayload, "payload must be valid UTF-8") ErrFrameTooLarge = newError(StatusTooLarge, "frame payload too large") ErrMessageTooLarge = newError(StatusTooLarge, "message paylaod too large") ErrOpcodeUnknown = newError(StatusProtocolError, "opcode unknown") ErrRSVBitsUnsupported = newError(StatusProtocolError, "RSV bits not supported") )
Protocol-level errors.
var Unmasked = MaskingKey([4]byte{})
Unmasked is the zero MaskingKey, indicating that a marshaled frame should not be masked.
Functions ¶
func MarshalFrame ¶ added in v0.0.2
func MarshalFrame(frame *Frame, mask MaskingKey) []byte
MarshalFrame marshals a Frame into bytes for transmission.
func WriteFrame ¶
func WriteFrame(dst io.Writer, mask MaskingKey, frame *Frame) error
WriteFrame writes a Frame to dst with the given masking key. To write an unmasked frame, use the special Unmasked key.
Types ¶
type ClientKey ¶
type ClientKey string
ClientKey identifies the client in the websocket handshake.
func Handshake ¶
Handshake is a low-level helper that validates the request and performs the WebSocket Handshake, after which only websocket frames should be written to the underlying connection.
Prefer the higher-level Accept API when possible.
func NewClientKey ¶
func NewClientKey() ClientKey
NewClientKey returns a randomly-generated ClientKey for client handshake. Panics on insufficient randomness.
type Error ¶ added in v0.0.2
type Error struct {
// contains filtered or unexported fields
}
Error is a websocket error with an associated StatusCode.
type Frame ¶
type Frame struct { Payload []byte // contains filtered or unexported fields }
Frame is a websocket frame.
func FrameMessage ¶ added in v0.0.2
FrameMessage splits a message into N frames with payloads of at most frameSize bytes.
func NewCloseFrame ¶ added in v0.0.2
func NewCloseFrame(code StatusCode, reason string) *Frame
NewCloseFrame creates a close Frame. If the given StatusCode is 0, the close frame will not have a payload.
func NewFrame ¶ added in v0.0.2
NewFrame creates a new Frame with the given Opcode, fin bit, and payload.
type Handler ¶
Handler handles a single websocket Message as part of the high level [Serve] request-response API.
If the returned message is non-nil, it will be sent to the client. If an error is returned, the connection will be closed.
type Hooks ¶
type Hooks struct { // OnClose is called when the connection is closed. OnClose func(ClientKey, StatusCode, error) // OnReadError is called when a read error occurs. OnReadError func(ClientKey, error) // OnReadFrame is called when a frame is read. OnReadFrame func(ClientKey, *Frame) // OnReadMessage is called when a complete message is read. OnReadMessage func(ClientKey, *Message) // OnWriteError is called when a write error occurs. OnWriteError func(ClientKey, error) // OnWriteFrame is called when a frame is written. OnWriteFrame func(ClientKey, *Frame) // OnWriteMessage is called when a complete message is written. OnWriteMessage func(ClientKey, *Message) }
Hooks define the callbacks that are called during the lifecycle of a websocket connection.
type MaskingKey ¶
type MaskingKey [4]byte
MaskingKey masks a client Frame.
func NewMaskingKey ¶
func NewMaskingKey() MaskingKey
NewMaskingKey returns a randomly generated MaskingKey for client frames. Panics on insufficient randomness.
type Message ¶
Message is an application-level message from the client, which may be constructed from one or more individual frames.
type Opcode ¶
type Opcode uint8
Opcode is a websocket OPCODE.
const ( OpcodeContinuation Opcode = 0b0000_0000 // 0x0 OpcodeText Opcode = 0b0000_0001 // 0x1 OpcodeBinary Opcode = 0b0000_0010 // 0x2 OpcodeClose Opcode = 0b0000_1000 // 0x8 OpcodePing Opcode = 0b0000_1001 // 0x9 OpcodePong Opcode = 0b0000_1010 // 0xA )
See the RFC for the set of defined opcodes: https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
type Options ¶
type Options struct { Hooks Hooks ReadTimeout time.Duration WriteTimeout time.Duration MaxFrameSize int MaxMessageSize int }
Options define the limits imposed on a websocket connection.
type StatusCode ¶
type StatusCode uint16
StatusCode is a websocket close status code.
const ( StatusNormalClosure StatusCode = 1000 StatusGoingAway StatusCode = 1001 StatusProtocolError StatusCode = 1002 StatusUnsupportedData StatusCode = 1003 StatusNoStatusRcvd StatusCode = 1005 StatusAbnormalClose StatusCode = 1006 StatusInvalidFramePayload StatusCode = 1007 StatusPolicyViolation StatusCode = 1008 StatusTooLarge StatusCode = 1009 StatusMandatoryExtension StatusCode = 1010 StatusInternalError StatusCode = 1011 StatusServiceRestart StatusCode = 1012 StatusTryAgainLater StatusCode = 1013 StatusGatewayError StatusCode = 1014 StatusTlSHandshake StatusCode = 1015 )
Close status codes, as defined in RFC 6455 and the IANA registry.
https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1 https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
type Websocket ¶
type Websocket struct {
// contains filtered or unexported fields
}
Websocket is a websocket connection.
func Accept ¶
Accept handles the initial HTTP-based handshake and upgrades the TCP connection to a websocket connection.
func New ¶
New is a low-level API that manually creates a new websocket connection. Caller is responsible for completing initial handshake before creating a websocket connection.
Prefer the higher-level Accept API when possible. See also Handshake if using New directly.
func (*Websocket) ReadMessage ¶
ReadMessage reads a single Message from the connection, handling fragments and control frames automatically. The connection will be closed on any error.
func (*Websocket) Serve ¶
Serve is a high-level convienience method for request-response style websocket connections, where the given Handler is called for each incoming message and its return value is sent back to the client.
See also EchoHandler.
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
testing/assert
Package assert implements common assertions used in go-httbin's unit tests.
|
Package assert implements common assertions used in go-httbin's unit tests. |
testing/must
Package must implements helper functions for testing to eliminate some error checking boilerplate.
|
Package must implements helper functions for testing to eliminate some error checking boilerplate. |