ws: github.com/gobwas/ws Index | Files | Directories

package ws

import "github.com/gobwas/ws"

Package ws implements a client and server for the WebSocket protocol as specified in RFC 6455.

The main purpose of this package is to provide simple low-level API for efficient work with protocol.

Overview.

Upgrade to WebSocket (or WebSocket handshake) can be done in two ways.

The first way is to use `net/http` server:

  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	  conn, err := ws.UpgradeHTTP(r, w, nil)
  })

The second and much more efficient way is so-called "zero-copy upgrade". It avoids redundant allocations and copying of not used headers or other request data. User decides by himself which data should be copied.

  ln, err := net.Listen("tcp", ":8080")
  if err != nil {
	  // handle error
  }

  conn, err := ln.Accept()
  if err != nil {
	  // handle error
  }

  handshake, err := ws.Upgrade(conn)
  if err != nil {
	  // handle error
  }

For customization details see `ws.Upgrader` documentation.

After WebSocket handshake you can work with connection in multiple ways. That is, `ws` does not force the only one way of how to work with WebSocket:

  header, err := ws.ReadHeader(conn)
  if err != nil {
	  // handle err
  }

  buf := make([]byte, header.Length)
  _, err := io.ReadFull(conn, buf)
  if err != nil {
	  // handle err
  }

  resp := ws.NewBinaryFrame([]byte("hello, world!"))
  if err := ws.WriteFrame(conn, frame); err != nil {
      // handle err
  }

As you can see, it stream friendly:

  const N = 42

  ws.WriteHeader(ws.Header{
	  Fin:    true,
	  Length: N,
	  OpCode: ws.OpBinary,
  })

  io.CopyN(conn, rand.Reader, N)

Or:

  header, err := ws.ReadHeader(conn)
  if err != nil {
	  // handle err
  }

  io.CopyN(ioutil.Discard, conn, header.Length)

For more info see the documentation.

Index

Package Files

check.go cipher.go client.go frame.go http.go read.go request.go server.go util.go write.go ws.go

Constants

const (
    DefaultReadBufferSize  = 4096
    DefaultWriteBufferSize = 512
)

Constants used by ConnUpgrader.

const (
    MaxHeaderSize = 14
    MinHeaderSize = 2
)
const (
    // All control frames MUST have a payload length of 125 bytes or less and MUST NOT be fragmented.
    MaxControlFramePayloadSize = 125
)

Constants defined by specification.

const (
    PlatformSizeLimit = int64(^(uint(0)) >> 1) // Max int value for current platform.
)

Variables

var (
    ErrProtocolOpCodeReserved             = ProtocolError(fmt.Errorf("use of reserved op code"))
    ErrProtocolControlPayloadOverflow     = ProtocolError(fmt.Errorf("control frame payload limit exceeded"))
    ErrProtocolControlNotFinal            = ProtocolError(fmt.Errorf("control frame is not final"))
    ErrProtocolNonZeroRsv                 = ProtocolError(fmt.Errorf("non-zero rsv bits with no extension negotiated"))
    ErrProtocolMaskRequired               = ProtocolError(fmt.Errorf("frames from client to server must be masked"))
    ErrProtocolMaskUnexpected             = ProtocolError(fmt.Errorf("frames from server to client must be not masked"))
    ErrProtocolContinuationExpected       = ProtocolError(fmt.Errorf("unexpected non-continuation data frame"))
    ErrProtocolContinuationUnexpected     = ProtocolError(fmt.Errorf("unexpected continuation data frame"))
    ErrProtocolStatusCodeNotInUse         = ProtocolError(fmt.Errorf("status code is not in use"))
    ErrProtocolStatusCodeApplicationLevel = ProtocolError(fmt.Errorf("status code is only application level"))
    ErrProtocolStatusCodeNoMeaning        = ProtocolError(fmt.Errorf("status code has no meaning yet"))
    ErrProtocolStatusCodeUnknown          = ProtocolError(fmt.Errorf("status code is not defined in spec"))
    ErrProtocolInvalidUTF8                = ProtocolError(fmt.Errorf("invalid utf8 sequence in close reason"))
)

Errors used by the protocol checkers.

var (
    ErrBadStatus      = fmt.Errorf("unexpected http status")
    ErrBadSubProtocol = fmt.Errorf("unexpected protocol in %q header", headerSecProtocol)
    ErrBadExtensions  = fmt.Errorf("unexpected extensions in %q header", headerSecProtocol)
)

Errors used by the websocket client.

var (
    StatusRangeNotInUse    = StatusCodeRange{0, 999}
    StatusRangeProtocol    = StatusCodeRange{1000, 2999}
    StatusRangeApplication = StatusCodeRange{3000, 3999}
    StatusRangePrivate     = StatusCodeRange{4000, 4999}
)

Status code ranges defined by specification. See https://tools.ietf.org/html/rfc6455#section-7.4.2

var (
    PingFrame  = Frame{Header{Fin: true, OpCode: OpPing}, nil}
    PongFrame  = Frame{Header{Fin: true, OpCode: OpPong}, nil}
    CloseFrame = Frame{Header{Fin: true, OpCode: OpClose}, nil}
)

Common frames with no special meaning.

var (
    CompiledPing  = MustCompileFrame(PingFrame)
    CompiledPong  = MustCompileFrame(PongFrame)
    CompiledClose = MustCompileFrame(CloseFrame)
)

Compiled control frames for common use cases. For construct-serialize optimizations.

var (
    ErrMalformedHttpRequest  = fmt.Errorf("malformed HTTP request")
    ErrMalformedHttpResponse = fmt.Errorf("malformed HTTP response")

    ErrBadHttpRequestProto  = fmt.Errorf("bad HTTP request protocol version")
    ErrBadHttpRequestMethod = fmt.Errorf("bad HTTP request method")

    ErrBadHost       = fmt.Errorf("bad %q header", headerHost)
    ErrBadUpgrade    = fmt.Errorf("bad %q header", headerUpgrade)
    ErrBadConnection = fmt.Errorf("bad %q header", headerConnection)
    ErrBadSecAccept  = fmt.Errorf("bad %q header", headerSecAccept)
    ErrBadSecKey     = fmt.Errorf("bad %q header", headerSecKey)
    ErrBadSecVersion = fmt.Errorf("bad %q header", headerSecVersion)
)
var (
    ErrHeaderLengthMSB        = fmt.Errorf("header error: the most significant bit must be 0")
    ErrHeaderLengthUnexpected = fmt.Errorf("header error: unexpected payload length bits")
)

Errors used by frame reader.

var ErrBadNonce = fmt.Errorf("nonce size is not %d", nonceSize)
var ErrNotHijacker = fmt.Errorf("given http.ResponseWriter is not a http.Hijacker")
var WebSocketMagic = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")

func CheckCloseFrameData Uses

func CheckCloseFrameData(code StatusCode, reason string) error

CheckCloseFrameData checks received close information to be valid RFC6455 compatible close info.

Note that code.Empty() or code.IsAppLevel() will raise error.

If endpoint sends close frame without status code (with frame.Length = 0), application should not check its payload.

func CheckHeader Uses

func CheckHeader(h Header, s State) error

CheckHeader checks h to contain valid header data for given state s.

Note that zero state (0) means that state is clean, neither server or client side, nor fragmented, nor extended.

func Cipher Uses

func Cipher(payload []byte, mask [4]byte, offset int)

Cipher applies XOR cipher to the payload using mask. Offset is used to cipher chunked data (e.g. in io.Reader implementations).

To convert masked data into unmasked data, or vice versa, the following algorithm is applied. The same algorithm applies regardless of the direction of the translation, e.g., the same steps are applied to mask the data as to unmask the data.

func CompileFrame Uses

func CompileFrame(f Frame) (bts []byte, err error)

CompileFrame returns byte representation of given frame. In terms of memory consumption it is useful to precompile static frames which are often used.

func Dial Uses

func Dial(ctx context.Context, urlstr string, h http.Header) (conn net.Conn, resp Response, err error)

Dial is like Dialer{}.Dial().

func HeaderSize Uses

func HeaderSize(h Header) (n int)

HeaderSize returns number of bytes that are needed to encode given header. It returns -1 if header is malformed.

func HeaderWriter Uses

func HeaderWriter(h http.Header) func(io.Writer)

HeaderWriter creates callback function that will dump h into recevied io.Writer inside created callback.

func MustCompileFrame Uses

func MustCompileFrame(f Frame) []byte

MustCompileFrame is like CompileFrame but panics if frame cannot be encoded.

func NewCloseFrameData Uses

func NewCloseFrameData(code StatusCode, reason string) []byte

NewCloseFrameData makes byte representation of code and reason.

Note that returned slice is at most 125 bytes length. If reason is too big it will crop it to fit the limit defined by thte spec.

See https://tools.ietf.org/html/rfc6455#section-5.5

func NewMask Uses

func NewMask() (ret [4]byte)

NewMask creates new random mask.

func PutCloseFrameData Uses

func PutCloseFrameData(p []byte, code StatusCode, reason string) int

PutCloseFrameData encodes code and reason into buf and returns the number of bytes written. If the buffer is too small to accommodate at least code, PutCloseFrameData will panic. Note that it does not checks maximum control frame payload size limit.

func Rsv Uses

func Rsv(r1, r2, r3 bool) (rsv byte)

Rsv creates rsv byte representation.

func SelectEqual Uses

func SelectEqual(v string) func(string) bool

SelectEqual creates accept function that could be used as Protocol/Extension select during upgrade.

func SelectFromSlice Uses

func SelectFromSlice(accept []string) func(string) bool

SelectFromSlice creates accept function that could be used as Protocol/Extension select during upgrade.

func UpgradeHTTP Uses

func UpgradeHTTP(r *http.Request, w http.ResponseWriter, h http.Header) (conn net.Conn, rw *bufio.ReadWriter, hs Handshake, err error)

UpgradeHTTP is like HTTPUpgrader{}.Upgrade().

func WriteFrame Uses

func WriteFrame(w io.Writer, f Frame) error

func WriteHeader Uses

func WriteHeader(w io.Writer, h Header) error

type Dialer Uses

type Dialer struct {
    // Protocol is the list of subprotocol names the client wishes to speak, ordered by preference.
    // See https://tools.ietf.org/html/rfc6455#section-4.1
    Protocol []string

    // Extensions is the list of extensions, that client wishes to speak.
    // See https://tools.ietf.org/html/rfc6455#section-4.1
    // See https://tools.ietf.org/html/rfc6455#section-9.1
    Extensions []httphead.Option

    // NetDial is the function that is used to get plain tcp connection.
    // If it is not nil, then it is used instead of net.Dialer.
    NetDial func(ctx context.Context, network, addr string) (net.Conn, error)

    // NetDialTLS is the function that is used to get plain tcp connection with tls encryption.
    // If it is not nil, then it is used instead of tls.DialWithDialer.
    NetDialTLS func(ctx context.Context, network, addr string) (net.Conn, error)

    // TLSConfig is passed to tls.DialWithDialer.
    TLSConfig *tls.Config

    // WriterPool is used to reuse bufio.Writers.
    WriterPool WriterPool

    // ReaderPool is used to reuse bufio.Readers.
    ReaderPool ReaderPool
}

Dialer contains options for establishing websocket connection to an url.

var DefaultDialer Dialer

DefaultDialer is dialer that holds no options and is used by Dial function.

func (Dialer) Dial Uses

func (d Dialer) Dial(ctx context.Context, urlstr string, h http.Header) (conn net.Conn, resp Response, err error)

Dial connects to the url host and handshakes connection to websocket. Set of additional headers could be passed to be sent with the request.

type Frame Uses

type Frame struct {
    Header  Header
    Payload []byte
}

Frame represents websocket frame. See https://tools.ietf.org/html/rfc6455#section-5.2

func MaskFrame Uses

func MaskFrame(f Frame) Frame

MaskFrame masks frame and returns frame with masked payload and Mask header's field set. Note that it copies f payload to prevent collisions. For less allocations you could use MaskFrameInPlace or construct frame manually.

func MaskFrameInPlace Uses

func MaskFrameInPlace(f Frame) Frame

MaskFrame masks frame and returns frame with masked payload and Mask header's field set. Note that it applies xor cipher to f.Payload without copying, that is, it modifies f.Payload inplace.

func MaskFrameInPlaceWith Uses

func MaskFrameInPlaceWith(f Frame, m [4]byte) Frame

MaskFrameInPlaceWith masks frame with given mask and returns frame with masked payload and Mask header's field set. Note that it applies xor cipher to f.Payload without copying, that is, it modifies f.Payload inplace.

func MaskFrameWith Uses

func MaskFrameWith(f Frame, mask [4]byte) Frame

MaskFrameWith masks frame with given mask and returns frame with masked payload and Mask header's field set. Note that it copies f payload to prevent collisions. For less allocations you could use MaskFrameInPlaceWith or construct frame manually.

func NewBinaryFrame Uses

func NewBinaryFrame(p []byte) Frame

NewBinaryFrame creates binary frame with p as payload. Note that p is left as is in the returned frame without copying.

func NewCloseFrame Uses

func NewCloseFrame(code StatusCode, reason string) Frame

NewCloseFrame creates close frame with given closure code and reason. Note that it crops reason to fit the limit of control frames payload. See https://tools.ietf.org/html/rfc6455#section-5.5

func NewFrame Uses

func NewFrame(op OpCode, fin bool, p []byte) Frame

NewFrame creates frame with given operation code, flag of completeness and payload bytes.

func NewPingFrame Uses

func NewPingFrame(p []byte) Frame

NewPingFrame creates ping frame with p as payload. Note that p is left as is in the returned frame without copying.

func NewPongFrame Uses

func NewPongFrame(p []byte) Frame

NewPongFrame creates pong frame with p as payload. Note that p is left as is in the returned frame.

func NewTextFrame Uses

func NewTextFrame(s string) Frame

NewTextFrame creates text frame with s as payload. Note that the s is copied in the returned frame payload.

func ReadFrame Uses

func ReadFrame(r io.Reader) (f Frame, err error)

ReadFrame reads a frame from r. It is not designed for high optimized use case cause it makes allocation for frame.Header.Length size inside to read frame payload into.

Note that ReadFrame does not unmask payload.

type HTTPUpgrader Uses

type HTTPUpgrader struct {
    // Protocol is the select function that is used to select subprotocol from
    // list requested by client. If this field is set, then the first matched
    // protocol is sent to a client as negotiated.
    Protocol func(string) bool

    // Extension is the select function that is used to select extensions from
    // list requested by client. If this field is set, then the all matched
    // extensions are sent to a client as negotiated.
    Extension func(httphead.Option) bool
}

HTTPUpgrader contains options for upgrading connection to websocket from net/http Handler arguments.

var DefaultHTTPUpgrader HTTPUpgrader

DefaultHTTPUpgrader is an HTTPUpgrader that holds no options and is used by UpgradeHTTP function.

func (HTTPUpgrader) Upgrade Uses

func (u HTTPUpgrader) Upgrade(r *http.Request, w http.ResponseWriter, h http.Header) (conn net.Conn, rw *bufio.ReadWriter, hs Handshake, err error)

Upgrade upgrades http connection to the websocket connection. Set of additional headers could be passed to be sent with the response after successful upgrade.

It hijacks net.Conn from w and returns recevied net.Conn and bufio.ReadWriter. On successful handshake it returns Handshake struct describing handshake info.

type Handshake Uses

type Handshake struct {
    // Protocol is the selected during handshake subprotocol.
    Protocol string

    // Extensions is the list of negotiated extensions.
    Extensions []httphead.Option
}

Handshake represents handshake result.

func Upgrade Uses

func Upgrade(conn io.ReadWriter) (Handshake, error)

Upgrade is like Upgrader{}.Upgrade().

type Header struct {
    Fin    bool
    Rsv    byte
    OpCode OpCode
    Length int64
    Masked bool
    Mask   [4]byte
}

Header represents websocket frame header. See https://tools.ietf.org/html/rfc6455#section-5.2

func ReadHeader Uses

func ReadHeader(r io.Reader) (h Header, err error)

ReadHeader reads a frame header from r.

func (Header) Rsv1 Uses

func (h Header) Rsv1() bool

Rsv1 reports whether the header has first rsv bit set.

func (Header) Rsv2 Uses

func (h Header) Rsv2() bool

Rsv2 reports whether the header has second rsv bit set.

func (Header) Rsv3 Uses

func (h Header) Rsv3() bool

Rsv3 reports whether the header has third rsv bit set.

type OpCode Uses

type OpCode byte

OpCode represents operation code.

const (
    OpContinuation OpCode = 0x0
    OpText                = 0x1
    OpBinary              = 0x2
    OpClose               = 0x8
    OpPing                = 0x9
    OpPong                = 0xa
)

Operation codes defined by specification. See https://tools.ietf.org/html/rfc6455#section-5.2

func (OpCode) IsControl Uses

func (c OpCode) IsControl() bool

IsControl checks wheter the c is control operation code. See https://tools.ietf.org/html/rfc6455#section-5.5

func (OpCode) IsData Uses

func (c OpCode) IsData() bool

IsData checks wheter the c is data operation code. See https://tools.ietf.org/html/rfc6455#section-5.6

func (OpCode) IsReserved Uses

func (c OpCode) IsReserved() bool

IsReserved checks wheter the c is reserved operation code. See https://tools.ietf.org/html/rfc6455#section-5.2

type ProtocolError Uses

type ProtocolError error

ProtocolError describes error during checking/parsing websocket frames or headers.

type ReaderPool Uses

type ReaderPool interface {
    Get(io.Reader) *bufio.Reader
    Put(*bufio.Reader)
}

ReaderPool describes object that manages reuse of bufio.Reader instances.

type Response Uses

type Response struct {
    *http.Response
    Handshake
}

Response represents result of dialing.

type State Uses

type State uint8

State represents state of websocket endpoint. It used by some functions to be more strict when checking compatibility with RFC6455.

const (
    // StateServerSide means that endpoint (caller) is a server.
    StateServerSide State = 0x1 << iota
    // StateServerSide means that endpoint (caller) is a client.
    StateClientSide
    // StateExtended means that extension was negotiated during handshake.
    StateExtended
    // StateFragmented means that endpoint (caller) has received fragmented
    // frame and waits for continuation parts.
    StateFragmented
)

func (State) Clear Uses

func (s State) Clear(v State) State

Clear disables v state on s.

func (State) Is Uses

func (s State) Is(v State) bool

Is checks whether the s has v enabled.

func (State) Set Uses

func (s State) Set(v State) State

Set enables v state on s.

func (State) SetOrClearIf Uses

func (s State) SetOrClearIf(cond bool, v State) (ret State)

SetOrClearIf enables or disables v state on s depending on cond.

type StatusCode Uses

type StatusCode uint16

StatusCode represents the encoded reason for closure of websocket connection.

There are few helper methods on StatusCode that helps to define a range in which given code is lay in. accordingly to ranges defined in specification.

See https://tools.ietf.org/html/rfc6455#section-7.4

const (
    StatusNormalClosure           StatusCode = 1000
    StatusGoingAway                          = 1001
    StatusProtocolError                      = 1002
    StatusUnsupportedData                    = 1003
    StatusNoMeaningYet                       = 1004
    StatusNoStatusRcvd                       = 1005
    StatusAbnormalClosure                    = 1006
    StatusInvalidFramePayloadData            = 1007
    StatusPolicyViolation                    = 1008
    StatusMessageTooBig                      = 1009
    StatusMandatoryExt                       = 1010
    StatusInternalServerError                = 1011
    StatusTLSHandshake                       = 1015
)

Status codes defined by specification. See https://tools.ietf.org/html/rfc6455#section-7.4.1

func ParseCloseFrameData Uses

func ParseCloseFrameData(payload []byte) (code StatusCode, reason string)

ParseCloseFrameData parses close frame status code and closure reason if any provided. If there is no status code in the payload the empty status code is returned (code.Empty()) with empty string as a reason.

func ParseCloseFrameDataUnsafe Uses

func ParseCloseFrameDataUnsafe(payload []byte) (code StatusCode, reason string)

ParseCloseFrameDataUnsafe is like ParseCloseFrameData except the thing that it does not copies payload bytes into reason, but prepares unsafe cast.

func (StatusCode) Empty Uses

func (s StatusCode) Empty() bool

Empty reports wheter the code is empty. Empty code has no any meaning neither app level codes nor other. This method is useful just to check that code is golang default value 0.

func (StatusCode) In Uses

func (s StatusCode) In(r StatusCodeRange) bool

In reports whether the code is defined in given range.

func (StatusCode) IsApplicationSpec Uses

func (s StatusCode) IsApplicationSpec() bool

IsApplicationSpec reports whether the code should be defined by application, framework or libraries specification.

func (StatusCode) IsNotUsed Uses

func (s StatusCode) IsNotUsed() bool

IsNotUsed reports whether the code is predefined in not used range.

func (StatusCode) IsPrivateSpec Uses

func (s StatusCode) IsPrivateSpec() bool

IsPrivateSpec reports whether the code should be defined privately.

func (StatusCode) IsProtocolDefined Uses

func (s StatusCode) IsProtocolDefined() bool

IsProtocolDefined reports whether the code is already defined by protocol specification.

func (StatusCode) IsProtocolReserved Uses

func (s StatusCode) IsProtocolReserved() bool

IsProtocolReserved reports whether the code is defined by protocol specification to be reserved only for application usage purpose.

func (StatusCode) IsProtocolSpec Uses

func (s StatusCode) IsProtocolSpec() bool

IsProtocolSpec reports whether the code should be defined by protocol specification.

type StatusCodeRange Uses

type StatusCodeRange struct {
    Min, Max StatusCode
}

StatusCodeRange describes range of StatusCode values.

type Upgrader Uses

type Upgrader struct {
    // ReadBufferSize and WriteBufferSize is an I/O buffer sizes.
    // They used to read and write http data while upgrading to WebSocket.
    // Allocated buffers are pooled with sync.Pool to avoid allocations.
    //
    // If *bufio.ReadWriter is given to Upgrade() no allocation will be made
    // and this sizes will not be used.
    //
    // If a size is zero then default value is used.
    //
    // Usually it is useful to set read buffer size bigger than write buffer
    // size because incoming request could contain long header values, such
    // Cookie. Response, in other way, could be big only if user write multiple
    // custom headers. Usually response takes less than 256 bytes.
    ReadBufferSize, WriteBufferSize int

    // Protocol is a select function that is used to select subprotocol
    // from list requested by client. If this field is set, then the first matched
    // protocol is sent to a client as negotiated.
    //
    // The argument is only valid until the callback returns.
    Protocol func([]byte) bool

    // ProtocolCustrom allow user to parse Sec-WebSocket-Protocol header manually.
    // Note that returned bytes must be valid until Upgrade returns.
    // If ProtocolCustom is set, it used instead of Protocol function.
    ProtocolCustom func([]byte) (string, bool)

    // Extension is a select function that is used to select extensions
    // from list requested by client. If this field is set, then the all matched
    // extensions are sent to a client as negotiated.
    //
    // The argument is only valid until the callback returns.
    //
    // According to the RFC6455 order of extensions passed by a client is
    // significant. That is, returning true from this function means that no
    // other extension with the same name should be checked because server
    // accepted the most preferable extension right now:
    // "Note that the order of extensions is significant.  Any interactions between
    // multiple extensions MAY be defined in the documents defining the extensions.
    // In the absence of such definitions, the interpretation is that the header
    // fields listed by the client in its request represent a preference of the
    // header fields it wishes to use, with the first options listed being most
    // preferable."
    Extension func(httphead.Option) bool

    // ExtensionCustorm allow user to parse Sec-WebSocket-Extensions header manually.
    // Note that returned options should be valid until Upgrade returns.
    // If ExtensionCustom is set, it used instead of Extension function.
    ExtensionCustom func([]byte, []httphead.Option) ([]httphead.Option, bool)

    // Header is a callback that will be called with io.Writer.
    // Write() calls that writer will put data in the response http headers
    // section.
    //
    // It used instead of http.Header mapping to avoid allocations in user land.
    //
    // Not that if present, this callback will be called for any result of
    // upgrading.
    Header func(io.Writer)

    // OnRequest is a callback that will be called after request line and
    // "Host" header successful parsing. Setting this field helps to implement
    // some application logic.
    //
    // The arguments are only valid until the callback returns.
    //
    // Returned value could be used to prevent processing request and response
    // with appropriate http status.
    OnRequest func(host, uri []byte) (err error, code int)

    // OnHeader is a callback that will be called after successful parsing of
    // header, that is not used during WebSocket handshake procedure. That is,
    // it will be called with non-websocket headers, which could be relevant
    // for application-level logic.
    //
    // The arguments are only valid until the callback returns.
    //
    // Returned value could be used to prevent processing request and response
    // with appropriate http status.
    OnHeader func(key, value []byte) (err error, code int)

    // BeforeUpgrade is a callback that will be called before sending
    // successful upgrade response.
    //
    // Setting BeforeUpgrade allows user to make final application-level
    // checks and decide whether this connection is allowed to successfully
    // upgrade to WebSocket. That is, the session checks and other application
    // logic could be contained inside this callback.
    //
    // BeforeUpgrade could return header writer callback, that will be called
    // to provide some user land http headers in response.
    //
    // If by some reason connection should not be upgraded then BeforeUpgrade
    // should return error and appropriate http status code.
    //
    // Note that header writer callback will be called even if err is non-nil.
    BeforeUpgrade func() (header func(io.Writer), err error, code int)
}

Upgrader contains options for upgrading connection to websocket.

var DefaultUpgrader Upgrader

DefaultUpgrader is an Upgrader that holds no options and is used by Upgrade function.

func (Upgrader) Upgrade Uses

func (u Upgrader) Upgrade(conn io.ReadWriter) (hs Handshake, err error)

Upgrade zero-copy upgrades connection to WebSocket. It interprets given conn as connection with incoming HTTP Upgrade request.

type WriterPool Uses

type WriterPool interface {
    Get(io.Writer) *bufio.Writer
    Put(*bufio.Writer)
}

WriterPool describes object that manages reuse of bufio.Writer instances.

Directories

PathSynopsis
wsutilPackage wsutil exports tools to simplify the work with WebSocket protocol.

Package ws imports 24 packages (graph) and is imported by 1 packages. Updated 2017-09-10. Refresh now. Tools for package owners.