websocket

package module
v0.0.0-...-a565c03 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

websocket provides an implementation of the WebSocket protocol in go.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRequestNotWebsocket indicates that the HTTP request provided does not specify
	// instructions for a WebSocket upgrade.
	ErrRequestNotWebsocket = errors.New("the request does not specify a websocket upgrade")
	// ErrVersionNotSupported indicates that the version provided in the request is not
	// supported. Currently supported versions: 13 (as per the RFC).
	ErrVersionNotSupported = errors.New("the request specifies an unsupported version")
	// ErrKeyNotProvided indicates that there is no Sec-WebSocket-Key passed in by the
	// client.
	ErrKeyNotProvided = errors.New("the request does not specify a Sec-WebSocket-Key")
	// ErrHijackFailed indicates an error with hijacking the underlying connection from
	// http.ResponseWriter.
	ErrHijackFailed = errors.New("unable to hijack the http connection")
	// ErrConnectionRead indicates an error reading from the underlying connection.
	ErrConnectionRead = errors.New("reading from the underlying connection failed")
	// ErrConnectionWrite indicates an error writing to the underlying connection.
	ErrConnectionWrite = errors.New("writing to the underlying connection failed")
	// ErrConnectionClosed indicates that the underlying connection is closed. This connection
	// cannot be read from or written to.
	ErrConnectionClosed = errors.New("connection is closed")
	// ErrMalformedFrame indicates that the server recieved an unexpectedly formed frame.
	ErrMalformedFrame = errors.New("websocket frame is malformed")
)

Functions

This section is empty.

Types

type Conn

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

Conn represents a WebSocket connection. All public methods on Conn are safe to be simultaneously called.

func AcceptHTTP

func AcceptHTTP(w http.ResponseWriter, r *http.Request) (*Conn, error)

AcceptHTTP handles a WebSocket HTTP request from the net/http client. It may return an error if the HTTP request is not a WebSocket connection, the WebSocket version is not supported, the Sec-WebSocket-Key is not provided, or hijacking the underlying connection fails.

func From

func From(c io.ReadWriteCloser) *Conn

From returns a new WebSocket Conn from a value with a type that implements the io.ReadWriteCloser interface, notably net.Conn. It is expected that this connection will not be read from, written to, or closed once passed into this function.

func (*Conn) Close

func (c *Conn) Close() error

Close marks the connection as closed and closes the underlying connection. It may return an error if there is an issue closing the underlying connection.

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) (bool, error)

Ping writes a ping frame to the connection. If a nil context is specified, it will default to five seconds. If no response is reached within the duration, it will return false. It may return an error if there is an issue writing to the connection.

If there is a ping that has not recieved a pong yet, calling this function will NOT write another ping frame, but will block until it recieves a pong.

func (*Conn) Read

func (c *Conn) Read() (*Message, error)

Read reads a WebSocket frame from the underlying connection. If there is an issue reading the frame or the frame is malformed, it may return an error.

func (*Conn) Write

func (c *Conn) Write(message *Message) error

Write takes in a message and writes it as a WebSocket frame to the underlying connection.

type Message

type Message struct {
	Type MessageType
	Data []byte
}

Message represents a WebSocket message.

func (Message) String

func (m Message) String() string

String returns the message as string formatted as: type: MessageType || data: MessageDataAsString

type MessageType

type MessageType uint8

MessageType represents the possible types of messages.

const (
	// MessageText is a UTF-8 encoded string.
	MessageText MessageType = 0
	// MessageBinary represents binary data.
	MessageBinary MessageType = 1
	// MessageClose represents a close handshake.
	MessageClose MessageType = 2
	// MessagePing represents a ping message from
	// the client.
	MessagePing MessageType = 3
	// MessagePong represents a pong message (usually
	// in return to a ping message).
	MessagePong MessageType = 4
)

func (MessageType) String

func (t MessageType) String() string

String returns the MessageType as a string.

Jump to

Keyboard shortcuts

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