ws

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeSignal

func DecodeSignal(data []byte) (*core.Signal, error)

DecodeSignal unmarshals a signal from JSON bytes.

func EncodeSignal

func EncodeSignal(signal core.Signal) ([]byte, error)

EncodeSignal marshals a signal to JSON bytes.

func NewInMemoryPair

func NewInMemoryPair(buffer int) (*InMemoryConn, *InMemoryConn)

NewInMemoryPair creates two connected in-memory WebSocket-like endpoints.

Types

type AcceptConfig

type AcceptConfig struct {
	AllowedOrigins []string
	Subprotocols   []string
}

AcceptConfig controls how a backend upgrades incoming HTTP requests to WebSocket connections.

type Acceptor

type Acceptor interface {
	Accept(w http.ResponseWriter, r *http.Request, cfg AcceptConfig) (Conn, error)
}

Acceptor upgrades inbound HTTP requests into framework connections.

type CoderAcceptor

type CoderAcceptor struct{}

CoderAcceptor upgrades HTTP requests using github.com/coder/websocket.

func NewCoderAcceptor

func NewCoderAcceptor() *CoderAcceptor

NewCoderAcceptor creates the default HTTP WebSocket acceptor.

func (*CoderAcceptor) Accept

type CoderConn

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

CoderConn wraps github.com/coder/websocket.Conn to implement the Conn interface.

func NewCoderConn

func NewCoderConn(conn *websocket.Conn) *CoderConn

NewCoderConn creates a new CoderConn from a coder/websocket connection.

func (*CoderConn) Close

func (c *CoderConn) Close() error

func (*CoderConn) Read

func (c *CoderConn) Read(ctx context.Context) ([]byte, error)

func (*CoderConn) SetReadLimit

func (c *CoderConn) SetReadLimit(limit int64)

SetReadLimit sets the maximum message size that can be read. Messages exceeding this limit will cause a read error.

func (*CoderConn) Write

func (c *CoderConn) Write(ctx context.Context, data []byte) error

type CoderDialer

type CoderDialer struct{}

CoderDialer implements Dialer using github.com/coder/websocket.

func (*CoderDialer) Dial

func (d *CoderDialer) Dial(ctx context.Context, url string) (Conn, error)

type Conn

type Conn interface {
	// Read reads a message from the connection.
	// It blocks until a message is available or the context is cancelled.
	Read(ctx context.Context) ([]byte, error)

	// Write writes a message to the connection.
	Write(ctx context.Context, data []byte) error

	// Close closes the connection.
	Close() error
}

Conn represents a WebSocket connection abstraction. Implementations must be safe for concurrent use.

type ConnHandler

type ConnHandler func(conn Conn)

ConnHandler is a function that handles new WebSocket connections.

type Dialer

type Dialer interface {
	Dial(ctx context.Context, url string) (Conn, error)
}

Dialer creates outbound WebSocket connections.

type Handler

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

Handler manages WebSocket connections including auth, rate limiting, and message reading.

func NewHandler

func NewHandler(cfg HandlerConfig) *Handler

NewHandler creates a new WebSocket handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles WebSocket upgrade requests. Implements http.Handler.

type HandlerConfig

type HandlerConfig struct {
	EnableAuth     bool
	JWTAuth        *auth.JWTAuth
	RateLimiter    *auth.RateLimiter
	MaxMessageSize int64
	AllowedOrigins []string
	Acceptor       Acceptor
	OnConnect      func(peerID, username, userRole string, conn Conn)
	OnDisconnect   func(peerID string)
	OnMessage      func(peerID, username, userRole string, data []byte)
}

HandlerConfig holds configuration for the WebSocket handler.

type InMemoryConn

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

InMemoryConn is an in-process Conn implementation useful for tests and embedded integrations.

func (*InMemoryConn) Close

func (c *InMemoryConn) Close() error

func (*InMemoryConn) Read

func (c *InMemoryConn) Read(ctx context.Context) ([]byte, error)

func (*InMemoryConn) Write

func (c *InMemoryConn) Write(ctx context.Context, data []byte) error

type InMemoryDialer

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

InMemoryDialer returns one side of a prebuilt in-memory pair on each Dial call.

func NewInMemoryDialer

func NewInMemoryDialer(conns ...Conn) *InMemoryDialer

NewInMemoryDialer creates a dialer backed by a fixed queue of in-memory connections.

func (*InMemoryDialer) Dial

func (d *InMemoryDialer) Dial(ctx context.Context, url string) (Conn, error)

type MessageType

type MessageType int

MessageType defines WebSocket message types.

const (
	TextMessage MessageType = iota + 1
	BinaryMessage
)

type ReadLimitConn

type ReadLimitConn interface {
	Conn
	SetReadLimit(limit int64)
}

ReadLimitConn is implemented by connections that support inbound frame limits.

Jump to

Keyboard shortcuts

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