websocket

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: Apache-2.0 Imports: 10 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 websocket sets up bitwire/1 connections over WebSockets: Accept and NewHandler serve the protocol at an HTTP endpoint, and Dial connects to one. Each returns an engine.Peer that owns its connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accept

func Accept(w http.ResponseWriter, r *http.Request, options ServerOptions) (*engine.Peer, error)

Accept upgrades an authenticated request to a WebSocket and speaks bitwire/1 over it. The caller must keep its HTTP handler alive until Peer.Done; NewHandler implements that lifetime contract.

func Dial

func Dial(ctx context.Context, url string, options DialOptions) (*engine.Peer, *http.Response, error)

Dial opens a WebSocket and speaks bitwire/1 over it. It uses ctx for the handshake and the connection lifetime: cancelling it disconnects the peer.

func NewHandler

func NewHandler(options ServerOptions) (http.Handler, error)

NewHandler serves bitwire/1 at an HTTP endpoint: each request that passes CheckOrigin and Authenticate is upgraded to a WebSocket and becomes a server-role peer with the options given.

Types

type DialOptions

type DialOptions struct {
	Options    engine.Options
	HTTPHeader http.Header
	HTTPClient *http.Client
	// ConnectTimeout bounds the handshake alone, defaulting to 30 seconds. A
	// dial that has not become a connection by then is refused with the code
	// connect_timeout and nothing is opened.
	ConnectTimeout time.Duration
	// Subprotocols are offered to the server in order of preference; the
	// default offers none. A browser refuses a handshake whose offer went
	// unselected, so a client that offers must be met by a server that
	// selects.
	Subprotocols []string
}

DialOptions is what Dial opens a WebSocket with: the peer's Options, the headers and client of the HTTP upgrade, the bound on the handshake, and the subprotocols to offer.

type ServerOptions

type ServerOptions struct {
	Options      engine.Options
	Authenticate func(*http.Request) (context.Context, error)
	CheckOrigin  func(*http.Request) bool
	// OnConnect is called with a peer that is already live: it has read
	// frames and may have answered them. What a peer must serve is attached
	// in Options.Prepare, which runs before it reads anything.
	OnConnect func(*engine.Peer)
	// Subprotocols are what the server will select, in its own order of
	// preference, from what a client offers; empty selects none, which is the
	// default. bitwire/1 names itself nowhere here and refuses nothing on
	// this ground.
	Subprotocols []string
	// SelectSubprotocol answers with the one subprotocol to select out of
	// what this request offered, "" for none. It is the selection, not a
	// filter over Subprotocols: a browser's ticket travels in the offer and
	// is accepted only if it is selected back unchanged. Nil selects the
	// first offered that Subprotocols names.
	SelectSubprotocol func(r *http.Request, offered []string) string
}

ServerOptions requires an explicit authentication and origin policy. The authenticated context is the base context for every incoming invocation.

Jump to

Keyboard shortcuts

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