wsutil

package
v0.9.10 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 14 Imported by: 1 module, 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 (
	DefaultPingInterval = 30 * time.Second
	DefaultPingTimeout  = 10 * time.Second
)

Variables

View Source
var ErrSubscriptionDropped = CloseWith(websocket.StatusTryAgainLater, "subscription dropped")

ErrSubscriptionDropped ends a session whose messenger's channel closed — the bus evicted a subscriber that stopped draining, or pubsub stopped — telling the client to try again later instead of leaving it on a connection that hears nothing.

Functions

func Bridge

func Bridge(conn *websocket.Conn, messenger model.Messenger, opts ...Option) error

Bridge relays JSON both ways between conn and messenger until the session ends, and returns what the handler returns: nil if the connection went first, else the close the session sent — a dropped subscription, a WithDone, an inbound error. It closes the messenger; conn stays the server's to close.

func Close

func Close(ctx context.Context, conn *websocket.Conn, err error) error

Close closes conn as the handler's err says: nil a normal closure; the peer's own close, an EOF, or a context's end — taken for a read's, which tore the connection down — nothing more; ctx done going away; a CloseWith its status and reason, whatever its cause; any other error an internal error, the one case it returns, for the caller to log. A close the handler's Bridge sent already is not sent again.

func CloseWith added in v0.9.8

func CloseWith(status websocket.StatusCode, reason string) error

CloseWith is the error a handler returns to have its connection closed with status and reason — a refusal, a displacement, a "try again later". Close sends them and reports no failure. The peer reads the reason: keep out of it what it should not.

func CloseWithCause added in v0.9.8

func CloseWithCause(status websocket.StatusCode, reason string, cause error) error

CloseWithCause is CloseWith keeping its cause: the peer gets the reason alone, the error's message says both for the logs, and errors.Is finds the cause.

func PeerGone added in v0.9.8

func PeerGone(err error) bool

PeerGone reports whether err means the connection went under a read: the peer hung up or sent a close frame, or the server closed the connection. A handler tells it from a refused handshake by PeerGone, to log the two apart; a context's end is neither, and ReadFirst returns it as ctx.Err().

func ReadFirst added in v0.9.8

func ReadFirst[T any](ctx context.Context, conn *websocket.Conn, timeout time.Duration, reason string) (T, error)

ReadFirst decodes the connection's first frame, JSON, into a T — a handshake such as a registration — waiting no longer than timeout. Short of a frame it returns one of three errors, the handler's to return:

  • the peer left first: the read's error as is, for Close to release the connection quietly; PeerGone reports it.
  • ctx ended first: ctx.Err(). The read under ctx has torn the connection down, so nothing more is sent. framingo's api server never ends a WebSocket request's context: on stop it closes the connection, and the read sees a peer gone.
  • no frame in time, or one that does not decode: CloseWith(StatusPolicyViolation, reason), reason naming the handshake.

A handler logging refusals skips the first by PeerGone and the second by ctx.Err(). Checking what the frame holds is the caller's, refusing one with CloseWithCause(StatusPolicyViolation, reason, why).

The read runs on a goroutine of its own rather than under a context with the timeout, since coder/websocket tears the connection down when a read's context ends; after a timeout it stays parked until the server closes the connection. The frame is decoded here, not by wsjson, which closes the connection itself on a bad one.

Types

type Option

type Option func(*session)

func WithDone added in v0.9.8

func WithDone(ch <-chan struct{}, err error) Option

WithDone ends the session once ch closes, closing with err — a CloseWith(StatusNormalClosure, "displaced") for a session a newer one claimed, for instance; nil is a normal closure. Given more than once, the first to close ends it.

func WithFilter added in v0.9.8

func WithFilter(filter func(msg entity.PubsubMessage) bool) Option

WithFilter writes only the bus messages filter keeps — the session's own, matched against its handshake, for instance. By default every message is written.

func WithFrame added in v0.9.8

func WithFrame(frame func(msg entity.PubsubMessage) any) Option

WithFrame writes what frame makes of a bus message, as JSON — its payload alone, for instance. By default the message itself is written.

func WithInbound added in v0.9.8

func WithInbound(inbound func(ctx context.Context, data []byte) error) Option

WithInbound hands each text or binary frame the client sends to inbound — an ack, for instance. An error ends the session with that close: a CloseWith's status and reason, an internal error for any other. By default a frame is relayed to the bus.

func WithLogger

func WithLogger(logger log.Logger) Option

func WithPing

func WithPing(interval, timeout time.Duration) Option

WithPing: interval 0 disables pings; a pong missing past timeout ends the session.

Jump to

Keyboard shortcuts

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