# README
Bhojpur API - WebSocket Library
The websocket
library provides high- and low- level bindings for the web browser's
WebSocket API.
Installation
go get github.com/bhojpur/api
Highlights
- Minimal and idiomatic API
- First class context.Context support
- Fully passes the WebSocket autobahn-testsuite
- Single dependency
- JSON and protobuf helpers in the wsjson and wspb sub-packages
- Zero alloc reads and writes
- Concurrent writes
- Close handshake
- net.Conn wrapper
- Ping pong API
- RFC 7692 permessage-deflate compression
- Compile to Wasm
Simple Usage
Server-side
http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
// ...
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
defer cancel()
var v interface{}
err = wsjson.Read(ctx, c, &v)
if err != nil {
// ...
}
log.Printf("received: %v", v)
c.Close(websocket.StatusNormalClosure, "")
})
Client-side
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
// ...
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
err = wsjson.Write(ctx, c, "hi")
if err != nil {
// ...
}
c.Close(websocket.StatusNormalClosure, "")
Some Examples
For a production quality example that demonstrates the complete API, check the echo example.
For a full stack example, see the chat example.
# Functions
Accept accepts a WebSocket handshake from a client and upgrades the the connection to a WebSocket.
CloseStatus is a convenience wrapper around Go 1.13's errors.
Dial performs a WebSocket handshake on url.
NetConn converts a *websocket.Conn into a net.Conn.
# Constants
CompressionContextTakeover uses a flate.Reader and flate.Writer per connection.
CompressionDisabled disables the deflate extension.
CompressionNoContextTakeover grabs a new flate.Reader and flate.Writer as needed for every message.
MessageBinary is for binary messages like protobufs.
MessageText is for UTF-8 encoded text messages like JSON.
StatusAbnormalClosure is exported for use only with Wasm.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
StatusNoStatusRcvd cannot be sent in a close message.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
StatusTLSHandshake is only exported for use with Wasm.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
https://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
These are only the status codes defined by the protocol.
# Structs
AcceptOptions represents Accept's options.
CloseError is returned when the connection is closed with a status and reason.
Conn represents a WebSocket connection.
DialOptions represents Dial's options.
# Type aliases
CompressionMode represents the modes available to the deflate extension.
MessageType represents the type of a WebSocket message.
StatusCode represents a WebSocket status code.