websocket

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2022 License: BSD-2-Clause Imports: 17 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ErrReceiveTimeout timeoutError = "receive"
	ErrSendTimeout    timeoutError = "send"
)
View Source
const WebSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

WebSocketGUID is a fixed GUID for websocket server to generate |Sec-WebSocket-Accept| header.

rfc 6455: section 4.1

Variables

View Source
var DefaultDialer = &defaultDialer
View Source
var DefaultServerConfig = &defaultServerConfig
View Source
var MaximumSegmentSize = 1024
View Source
var PortMap = map[string]string{
	"ws":  "80",
	"wss": "443",
}

Functions

func VerifyURI

func VerifyURI(uri string) (err error)

VerifyURI verifies whether provided uri is a valid websocket uri.

rfc 6455: section 3

Types

type CloseCode

type CloseCode uint16

CloseCode indicates the close status of a closed websocket connection, call CloseCode.String() to get the status text.

rfc 6455: section 7.4

const (
	NormalClosure           CloseCode = 1000
	GoingAway               CloseCode = 1001
	ProtocolError           CloseCode = 1002
	UnsupportedData         CloseCode = 1003
	NoStatusReceived        CloseCode = 1005
	AbnormalClosure         CloseCode = 1006
	InvalidFramePayloadData CloseCode = 1007
	PolicyViolation         CloseCode = 1008
	MessageTooBig           CloseCode = 1009
	MandatoryExtension      CloseCode = 1010
	InternalServerError     CloseCode = 1011
	TLSHandshake            CloseCode = 1015
)

func (CloseCode) String

func (c CloseCode) String() string

type ConnectionCloseError

type ConnectionCloseError interface {
	net.Error
	Code() CloseCode
	Msg() any
}

ConnectionCloseError contains all about an error caused by connection close.

type ConnectionState

type ConnectionState int

ConnectionState indicates the connection state of a websocket connection.

rfc 6455

const (
	Connecting ConnectionState = iota
	Open
	Closed
)

func (ConnectionState) String added in v1.1.1

func (s ConnectionState) String() string

type Dialer

type Dialer struct {
	// Origin should be contained for a web browser.
	Origin string
	// Protocol contains the sub-protocols to use.
	// server may choose one or none from them.
	Protocol []string
	// Extensions are websocket extensions to use.
	// server should support all of them.
	Extensions []string

	// Header contains original headers in the handshake request.
	Header http.Header

	// NetDialer is the net dialer for connect,
	// defaults to tls.NetDialer for "wss" scheme and net.NetDialer for "ws".
	NetDialer NetDialer
}

Dialer contains the config to start a websocket connection.

func (*Dialer) Dial

func (d *Dialer) Dial(url string) (ws WebSocket, err error)

Dial calls DialContext with background context.

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, url string) (ws WebSocket, err error)

DialContext starts a websocket connection to the url using provided context, returns a WebSocket connection. The provided context must be non-nil.

type Message

type Message struct {
	// Op is the Opcode of the message(the Opcode of the first frame).
	//
	// rfc 6455: section 5.2
	Op Opcode
	// Len indicates the content-length of the message(the total payload length of all frames).
	Len int64

	// Data is the message content(concatenate the payload of all frames).
	Data []byte
}

Message is a websocket message. To send a message, the Message.Len will be calculated from Message.Data and the original value will be discarded.

rfc 6455: section 1.2

type NetDialer

type NetDialer interface {
	Dial(network string, addr string) (net.Conn, error)
	DialContext(ctx context.Context, network string, addr string) (net.Conn, error)
}

NetDialer can hold net.Dialer or tls.Dialer

type Opcode

type Opcode uint8

Opcode indicates the operation of a websocket frame.

rfc 6455: section 5.2

const (
	OpContinuationFrame Opcode = 0x0
	OpTextFrame         Opcode = 0x1
	OpBinaryFrame       Opcode = 0x2

	OpConnectionClose Opcode = 0x8
	OpPing            Opcode = 0x9
	OpPong            Opcode = 0xa
)

func (Opcode) String

func (op Opcode) String() string

type ServerConfig

type ServerConfig struct {
	// Protocol contains protocols the server support.
	// May choose one of them for each connect request.
	Protocol []string
	// Extensions contains extensions the server support.
	// for each connection request, server should support all the client extensions.
	Extensions []string
}

func (*ServerConfig) Upgrade

func (srv *ServerConfig) Upgrade(w http.ResponseWriter, r *http.Request) (ws WebSocket, err error)

Upgrade takes over an existing http connection and returns a websocket connection

type WebSocket

type WebSocket interface {
	// State returns the state of the websocket connection.
	State() ConnectionState
	// Extensions returns extensions used by the websocket connection.
	Extensions() []string
	// Protocol returns the sub-protocol used by the websocket connection.
	Protocol() string

	// RecvCtx waits for a websocket message, and parses it into Message.
	// Frames with OpPing will be responded with a frame with OpPong.
	// Frames with OpConnectionClose will be responded and packed as ConnectionCloseError.
	RecvCtx(ctx context.Context) (msg Message, err error)
	// Recv works like WebSocket.RecvCtx but discards the frame metadata.
	Recv() (data []byte, err error)

	// SendCtx sends a message to the connection.
	SendCtx(ctx context.Context, msg Message) (err error)
	// Send sends a message with OpBinaryFrame to the connection.
	Send(data []byte) (err error)
	// SendText sends a message with OpTextFrame to the connection.
	SendText(txt string) (err error)
	// Ping sends a frame with OpPing to the connection and waits for the next frame,
	// typically a frame with OpPong, parses it and returns its body.
	Ping() (resp []byte, err error)

	// Close sends a frame with OpConnectionClose, waits for the response and closes the underlying
	// net.Conn. It does nothing if WebSocket is already closed or is closing.
	Close() (err error)
}

WebSocket is a websocket connection.

func Dial

func Dial(url string) (ws WebSocket, err error)

Dial connects to the url using DefaultDialer.

func Upgrade

func Upgrade(w http.ResponseWriter, r *http.Request) (WebSocket, error)

Jump to

Keyboard shortcuts

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