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.