websocket

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package websocket provides WebSocket connection support for services that use relay for HTTP communication. It applies the same authentication and signing primitives (relay.RequestSigner, custom headers) to the WebSocket upgrade handshake, ensuring a consistent authentication story across HTTP and WebSocket connections.

Basic usage

conn, err := websocket.Dial(ctx, "wss://api.example.com/ws",
    websocket.WithSigner(relay.RequestSignerFunc(func(r *http.Request) error {
        r.Header.Set("Authorization", "Bearer "+token)
        return nil
    })),
)
if err != nil { ... }
defer conn.Close()

// Send a message
if err := conn.WriteText("hello"); err != nil { ... }

// Receive a message
msg, err := conn.ReadMessage()

TLS customisation

conn, err := websocket.Dial(ctx, "wss://...",
    websocket.WithTLSConfig(&tls.Config{
        InsecureSkipVerify: false,
        RootCAs:            certPool,
    }),
)

Index

Constants

View Source
const (
	// TextMessage denotes a text data message (UTF-8 encoded).
	TextMessage = gorilla.TextMessage
	// BinaryMessage denotes a binary data message.
	BinaryMessage = gorilla.BinaryMessage
	// CloseMessage denotes a close control message.
	CloseMessage = gorilla.CloseMessage
	// PingMessage denotes a ping control message.
	PingMessage = gorilla.PingMessage
	// PongMessage denotes a pong control message.
	PongMessage = gorilla.PongMessage
)

MessageType mirrors gorilla/websocket message types for callers that need to distinguish text from binary frames.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn is an active WebSocket connection. Call Close or CloseGracefully when done.

func Dial

func Dial(ctx context.Context, url string, opts ...Option) (*Conn, error)

Dial establishes a WebSocket connection to url. It performs the HTTP upgrade handshake, applying any configured headers and signer before sending.

url must use the "ws://" or "wss://" scheme.

Dial blocks until the handshake completes or ctx is cancelled.

func (*Conn) Close

func (c *Conn) Close() error

Close sends a close frame and then closes the underlying connection immediately. For a graceful shutdown, use [CloseGracefully].

func (*Conn) CloseGracefully

func (c *Conn) CloseGracefully(code int, reason string) error

CloseGracefully sends a close control message with the given code and reason, then waits up to 5 seconds for the peer to echo a close frame before closing the underlying connection (RFC 6455 §7.1.2).

Use gorilla/websocket close codes (e.g. 1000 for normal closure, 1001 for going away).

func (*Conn) ReadMessage

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

ReadMessage reads the next message from the peer. It blocks until a message arrives, the connection is closed, or an error occurs.

func (*Conn) Underlying

func (c *Conn) Underlying() *gorilla.Conn

Underlying returns the underlying gorilla/websocket connection for advanced use cases (setting read/write deadlines, configuring ping handlers, etc.).

func (*Conn) WriteBytes

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

WriteBytes sends a binary frame to the peer.

func (*Conn) WriteText

func (c *Conn) WriteText(text string) error

WriteText sends a UTF-8 text frame to the peer.

type Message

type Message struct {
	// Type is the message type (TextMessage or BinaryMessage).
	Type int
	// Data is the raw message payload.
	Data []byte
}

Message is a WebSocket message received from the peer.

type Option

type Option func(*option)

Option configures a WebSocket Dial call.

func WithHeaders

func WithHeaders(h http.Header) Option

WithHeaders adds static headers to the WebSocket upgrade handshake. Calling WithHeaders multiple times merges all provided headers.

func WithSigner

func WithSigner(s relay.RequestSigner) Option

WithSigner sets a relay.RequestSigner that signs the upgrade HTTP request before the handshake is sent. This mirrors relay.WithSigner so the same signer implementation can be reused for both HTTP and WebSocket connections.

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) Option

WithTLSConfig sets a custom TLS configuration for the upgrade connection.

Jump to

Keyboard shortcuts

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