websocket

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package websocket upgrades HTTP connections to WebSocket connections and manages the resulting connection. It has no business logic: callers write their own read loop, decide what messages mean, and own any application concepts (rooms, hubs, auth) on top of the raw Conn.

WebSocket.Upgrade performs the HTTP-to-WebSocket handshake and returns a Conn. Conn.ReadMessage blocks for the next inbound frame and must be used from a single goroutine. Write, WriteMessage, and WriteJSON are safe for concurrent use: a mutex serializes them onto the underlying connection and each call blocks until its frame is written or WriteTimeout elapses. A background goroutine pings the peer every PingInterval; a peer that stops answering trips the read deadline, which surfaces as an error from ReadMessage.

Basic usage:

ws, err := websocket.New(&websocket.Config{AllowedOrigins: []string{"https://example.com"}})
if err != nil {
	log.Fatal(err)
}

e.GET("/ws", func(c echo.Context) error {
	conn, err := ws.Upgrade(c.Response(), c.Request())
	if err != nil {
		return err
	}
	defer conn.Close()

	for {
		_, data, err := conn.ReadMessage()
		if err != nil {
			return nil
		}
		// caller decides what data means
	}
})

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPayloadEmpty  = errors.New("websocket: payload is empty")
	ErrConfigNil     = errors.New("websocket: configuration must not be nil")
	ErrConfigInvalid = errors.New("websocket: configuration values must not be negative")
	ErrConnClosed    = errors.New("websocket: connection is closed")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	ReadBufferSize  int
	WriteBufferSize int
	PingInterval    time.Duration
	PingTimeout     time.Duration
	WriteTimeout    time.Duration
	ReadLimit       int64
	AllowedOrigins  []string
}

func (*Config) WithDefaults

func (cfg *Config) WithDefaults() *Config

type Conn

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

func (*Conn) Close

func (c *Conn) Close() error

Close sends a close frame, closes the underlying connection, and stops the ping loop. It is safe to call from any goroutine and more than once; every call returns the result of the first.

func (*Conn) ReadMessage

func (c *Conn) ReadMessage() (int, Payload, error)

func (*Conn) Write

func (c *Conn) Write(data []byte) error

func (*Conn) WriteJSON

func (c *Conn) WriteJSON(v any) error

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(messageType int, data []byte) error

type Payload

type Payload []byte

func (Payload) Unpack

func (p Payload) Unpack(v any) error

type WebSocket

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

func New

func New(cfg *Config) (*WebSocket, error)

func (*WebSocket) Upgrade

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

Jump to

Keyboard shortcuts

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