Documentation
¶
Index ¶
Constants ¶
const HttpMethod = specs.HttpMethodGet
Variables ¶
var ( ErrFailChallenge = specs.NewOpError("ws", "fail to complete dial challenge") ErrUnknownProtocol = specs.NewOpError("ws", "unknown websocket protocol") )
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn interface {
// RemoteAddr returns the remote network address of the connection.
RemoteAddr() net.Addr
// Protocol returns the protocol used by the WebSocket connection.
// Provided by the server during the handshake.
Protocol() string
// Alive checks if the WebSocket connection is still alive.
Alive() bool
// Read reads data from the WebSocket connection into the provided byte slice.
Read([]byte) (int, error)
// Write writes data to the WebSocket connection.
// The payload is expected to be a binary message.
Write([]byte) (int, error)
// WriteText writes a text message to the WebSocket connection.
// The payload is expected to be a UTF-8 encoded string.
WriteText(payload string) (int, error)
// WriteClose writes a close frame to the WebSocket connection with the specified close code.
WriteClose(WsCloseCode) error
// Close closes the WebSocket connection.
Close() error
}
Conn represents a WebSocket connection interface. It provides methods to interact with the connection, such as reading and writing data, setting deadlines, and checking the connection status.
type Dialer ¶
type Dialer struct {
// Origin is the value of the "Origin" header in the WebSocket handshake request.
Origin string
// Protocols is a list of subprotocols that the client supports.
Protocols []string
// EnableCompression indicates whether to enable 'permessage-deflate' extension for compression.
EnableCompression bool
// MaxFrameSize is the maximum size of a WebSocket frame in bytes.
MaxFrameSize int
// ReadTimeout indicates the maximum duration for reading messages from the WebSocket connection.
ReadTimeout time.Duration
// WriteTimeout indicates the maximum duration for writing messages to the WebSocket connection.
WriteTimeout time.Duration
// contains filtered or unexported fields
}
Dialer is used to create a WebSocket connection to a server.
func DefaultDialer ¶
func DefaultDialer() *Dialer
DefaultDialer returns a new Dialer with default settings.
type Upgrader ¶
type Upgrader struct {
// SelectProtocol is an optional function to select a protocol from the
// Sec-WebSocket-Protocol header.
//
// If nil, the first protocol from the header will be selected by default.
// if returned protocol is empty, the upgrade will fail with
SelectProtocol func(protocols []string) string
// EnableCompression indicates whether to enable WebSocket compression.
//
// If true, the server will support the 'permessage-deflate' extension.
EnableCompression bool
// MaxFrameSize is the maximum size of a WebSocket frame in bytes.
MaxFrameSize int
// ReadTimeout indicates the maximum duration for reading messages from the WebSocket connection.
ReadTimeout time.Duration
// WriteTimeout indicates the maximum duration for writing messages to the WebSocket connection.
WriteTimeout time.Duration
// contains filtered or unexported fields
}
Upgrader is used to upgrade HTTP requests to WebSocket connections.
func DefaultUpgrader ¶
func DefaultUpgrader() *Upgrader
DefaultUpgrader returns a default Upgrader instance with no custom protocol selection.
func (*Upgrader) Upgrade ¶
Upgrade upgrades an HTTP request to a WebSocket connection. It checks the request method, headers, and the Sec-WebSocket-Key. If the request is valid, it hijacks the connection and invokes the provided handler with a new WebSocket connection. The response is returned with the necessary headers to complete the WebSocket handshake.
type WsCloseCode ¶
type WsCloseCode uint16
WsCloseCode represents the WebSocket close codes as defined in RFC 6455.
const ( CloseCodeNormal WsCloseCode = 1000 CloseCodeGoingAway WsCloseCode = 1001 CloseCodeProtocolError WsCloseCode = 1002 CloseCodeUnsupportedData WsCloseCode = 1003 CloseCodeNoStatusReceived WsCloseCode = 1005 CloseCodeAbnormal WsCloseCode = 1006 CloseCodeInvalidPayloadData WsCloseCode = 1007 CloseCodePolicyViolation WsCloseCode = 1008 CloseCodeMessageTooBig WsCloseCode = 1009 CloseCodeMandatoryExtension WsCloseCode = 1010 CloseCodeInternalServerError WsCloseCode = 1011 CloseCodeServiceRestart WsCloseCode = 1012 CloseCodeTryAgainLater WsCloseCode = 1013 CloseCodeTLSHandshake WsCloseCode = 1015 )
Predefined WebSocket close codes as per RFC 6455. These codes are used to indicate the reason for closing a WebSocket connection.