ws

package
v2.11.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 14 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

Index

Constants

View Source
const DefaultConnectTimeout = 10 * time.Second

DefaultConnectTimeout is how long, with auth on, a socket may stay open without a connect that passed.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hub

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

Hub manages WebSocket connections and broadcasts messages using the Centrifugo v5 JSON protocol.

func NewHub

func NewHub() *Hub

func (*Hub) AddServerChannel

func (h *Hub) AddServerChannel(channel string)

AddServerChannel subscribes every client to channel on connect (server-side subscription), like the project channels.

func (*Hub) Broadcast

func (h *Hub) Broadcast(channel, eventName string, payload any)

Broadcast sends a publication push to all connected clients.

func (*Hub) HandleUpgrade

func (h *Hub) HandleUpgrade(w http.ResponseWriter, r *http.Request)

HandleUpgrade is the HTTP handler for /connection/websocket.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run keeps the hub alive until context is cancelled.

func (*Hub) SetMetrics

func (h *Hub) SetMetrics(m *metrics.Collector)

SetMetrics sets the metrics collector for the hub.

func (*Hub) SetProjectProvider

func (h *Hub) SetProjectProvider(p ProjectProvider)

SetProjectProvider sets the function that returns project keys for auto-subscription.

func (*Hub) SetRPCHandler

func (h *Hub) SetRPCHandler(handler RPCHandler)

SetRPCHandler sets the handler for Centrifugo RPC calls.

func (*Hub) SetTokenValidator

func (h *Hub) SetTokenValidator(v TokenValidator)

SetTokenValidator turns on auth for the socket: a connect needs a token v accepts, only connected clients get commands answered and pushes, a socket without a passed connect is closed after the connect timeout, and a socket is closed when its token expires. Call it before serving.

type MuxRPCHandler

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

MuxRPCHandler routes Centrifugo RPC calls to the HTTP mux.

func NewMuxRPCHandler

func NewMuxRPCHandler(mux *http.ServeMux) *MuxRPCHandler

func (*MuxRPCHandler) HandleRPC

func (h *MuxRPCHandler) HandleRPC(method, uri string, data json.RawMessage) (json.RawMessage, error)

HandleRPC converts an RPC call (e.g., DELETE api/event/{id}) into an HTTP request, executes it against the mux, and returns the response.

type ProjectProvider

type ProjectProvider func() []string

ProjectProvider returns the list of project keys for server-side subscriptions.

type RPCHandler

type RPCHandler interface {
	HandleRPC(method, uri string, data json.RawMessage) (json.RawMessage, error)
}

RPCHandler processes Centrifugo RPC calls (e.g., "delete:api/event/{id}").

type TokenValidator

type TokenValidator func(token string) (expires time.Time, ok bool)

TokenValidator reports whether an API auth token is valid and when it expires (zero time: never).

Jump to

Keyboard shortcuts

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