wsgateway

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wsgateway serves the realtime WebSocket endpoint GET /api/v1/ws: authentication (the request's principal) plus the Origin check, the hello frame, resume and replay from the event log, live delivery from the hub, heartbeats, client signal frames and the close codes of docs/protocol.md.

Index

Constants

View Source
const (
	DefaultPingInterval = 25 * time.Second
	DefaultDeadAfter    = 60 * time.Second
)

Heartbeat defaults (plan pipeline step 3).

Variables

This section is empty.

Functions

This section is empty.

Types

type Gateway

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

Gateway is the WebSocket handler. Mount it behind the auth middleware so the request carries its principal.

func New

func New(o Options) *Gateway

New returns a gateway.

func (*Gateway) CloseAll

func (g *Gateway) CloseAll(code int, reason string)

CloseAll closes every open socket with code and reason (4010 server_reset, 4011 content_cleared, 1001 on shutdown) and waits, bounded, until their handlers have finished.

func (*Gateway) Conns

func (g *Gateway) Conns() int

Conns returns the number of open sockets.

func (*Gateway) ServeHTTP

func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP authenticates and upgrades the request, then serves the connection until it closes. Browser (cookie) principals must send an Origin that matches the request host, which blocks cross-site WebSocket hijacking; bearer clients are exempt.

type Options

type Options struct {
	// Hub delivers live events. Required.
	Hub *events.Hub
	// DB holds the instance row and the event log for resume. Required.
	DB *store.DB
	// Version is reported as server_version in hello.
	Version string
	// Logger receives connection failures; nil discards them.
	Logger *slog.Logger
	// PingInterval is the server ping period; 0 means DefaultPingInterval.
	PingInterval time.Duration
	// DeadAfter closes a connection that answered no ping for this long
	// (and bounds a single write); 0 means DefaultDeadAfter.
	DeadAfter time.Duration
}

Options configures New.

Jump to

Keyboard shortcuts

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