websocket

package
v0.0.0-...-19586cb Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 16 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 serves and dials the WebSocket connections a stream travels on over HTTP: every message one text frame holding a JSON document, the client's last one followed by a frame holding null.

github.com/coder/websocket speaks the protocol. This package adds what a stream needs around it, and keeps the library out of every other package: a reader that ends the connection's context when the peer leaves, which the context of a request no longer does once its connection is taken over from the HTTP server; the server's shutdown ending that context too (see streamctx); a bound on each send and on the size of a message; a heartbeat; the close statuses the endings of a stream are told apart by; and Drain, the wait a shutdown gives the connections accepted, which http.Server.Shutdown neither waits for nor closes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Drain

func Drain(ctx context.Context) int

Drain waits for every connection Accept returned to be closed, for as long as ctx lasts, then cuts the ones still open, without the closing handshake, and returns how many it cut. A server calls it once it has begun to shut down: the connections' contexts have ended by then, and each stream closes its own with the status of its ending; http.Server.Shutdown returns without waiting for a connection taken over from it, and closes none.

func ValidateOrigins

func ValidateOrigins(origins []string) error

ValidateOrigins reports the first of origins the library could not read as a pattern, the way it reads the OriginPatterns Accept hands it: lower-cased, by path.Match. The library has no check of its own to call: it reads a pattern only once a handshake from another origin reaches it, and refuses that handshake over a malformed one, so a server validates the patterns it is configured with before it serves.

Types

type Conn

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

Conn is one WebSocket connection carrying JSON messages: the server end Accept returns, or the client end Dial returns. Its methods are safe for concurrent use, Recv by one goroutine at a time.

func Accept

func Accept(w http.ResponseWriter, r *http.Request, origins []string, writeTimeout time.Duration) (*Conn, error)

Accept upgrades the request to a WebSocket connection and returns its server end, which the caller closes when its stream is over. origins lists the patterns of the origins admitted beside the host the request is sent to, as the OriginPatterns of the library reads them, and writeTimeout bounds each Send, zero for no bound.

A handshake refused, a request that is none, an origin not admitted, a response that cannot be taken over, writes nothing: Accept returns the refusal as a service error carrying the status and the text the library refuses with, for the caller to answer the way it answers every refusal, and sets the headers the protocol answers such a request with.

func Dial

func Dial(ctx context.Context, client *http.Client, url string, header http.Header, writeTimeout time.Duration) (*Conn, *http.Response, error)

Dial opens a WebSocket connection to the server at url, an http or https address, and returns its client end, which the caller closes. client carries the handshake, http.DefaultClient when nil, with header among its headers; ctx bounds the handshake alone, and writeTimeout each Send, zero for no bound.

A handshake the server refuses fails, and the response returned is the server's answer to it, for the caller to read the refusal from: its body is the answer, the first maxRefusalBytes of it, read into memory. The response of a handshake accepted has no body, the connection being the Conn's. Neither is the caller's to close.

func (*Conn) Close

func (c *Conn) Close(err error) int

Close ends the connection with the status err stands for (see closeStatus), nil for an ending without an error, and waits for the peer to acknowledge it, briefly. Closing a connection again, or one that closed on what its peer sent, does nothing.

It returns the status the connection closed with: the one err stands for, or the one the connection had closed with before, its peer's when the peer closed it, StatusAbnormalClosure, 1006, when the peer left without closing.

func (*Conn) CloseRecv

func (c *Conn) CloseRecv()

CloseRecv tells the connection that Recv is called no more: Recv answers io.EOF from then on, and a message the peer sends from then on, or has sent and Recv has not taken, closes the connection with StatusPolicyViolation. An accepted connection still takes the frame its client ends its messages with. It is what the server of a stream whose client sends one message, or none, calls once it has what the client may send.

func (*Conn) CloseSend

func (c *Conn) CloseSend() error

CloseSend tells the server the client has sent its last message, on a dialed connection: the server's Recv answers io.EOF from then on, and a message sent after it closes the connection.

func (*Conn) Context

func (c *Conn) Context() context.Context

Context returns the connection's context. It ends when the peer leaves and when the connection is closed; that of an accepted connection ends with its request's as well, and the moment the server the request arrived on begins to shut down, with streamctx.ErrServerShutdown as its cause.

func (*Conn) Recv

func (c *Conn) Recv() ([]byte, error)

Recv returns the next message the peer sent. An accepted connection answers io.EOF once the client has sent its last message, whatever ends the connection after it. A dialed one answers io.EOF once the server has closed the connection without an error, and the error the server closed it with otherwise (see closeError). Once the connection's context has ended, Recv fails with its cause, and once CloseRecv has been called it answers io.EOF.

func (*Conn) Send

func (c *Conn) Send(message []byte) error

Send sends message, a JSON document, as one frame. It waits for the peer no longer than the connection's write timeout: a peer that does not read in time is cut off, the connection closed. Once the connection's context has ended, Send fails with its cause and sends nothing.

Jump to

Keyboard shortcuts

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