Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Oct 19, 2023
1 parent 50952d7 commit 9d9c971
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
23 changes: 12 additions & 11 deletions README.md
Expand Up @@ -5,15 +5,9 @@

websocket is a minimal and idiomatic WebSocket library for Go.

> **note**: I haven't been responsive for questions/reports on the issue tracker but I do
> read through and there are no outstanding bugs. There are certainly some nice to haves
> that I should merge in/figure out but nothing critical. I haven't given up on adding new
> features and cleaning up the code further, just been busy. Should anything critical
> arise, I will fix it.
## Install

```bash
```sh
go get nhooyr.io/websocket
```

Expand All @@ -23,18 +17,23 @@ go get nhooyr.io/websocket
- First class [context.Context](https://blog.golang.org/context) support
- Fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite)
- [Zero dependencies](https://pkg.go.dev/nhooyr.io/websocket?tab=imports)
- JSON and protobuf helpers in the [wsjson](https://pkg.go.dev/nhooyr.io/websocket/wsjson) and [wspb](https://pkg.go.dev/nhooyr.io/websocket/wspb) subpackages
- JSON helpers in the [wsjson](https://pkg.go.dev/nhooyr.io/websocket/wsjson) subpackage
- Zero alloc reads and writes
- Concurrent writes
- [Close handshake](https://pkg.go.dev/nhooyr.io/websocket#Conn.Close)
- [net.Conn](https://pkg.go.dev/nhooyr.io/websocket#NetConn) wrapper
- [Ping pong](https://pkg.go.dev/nhooyr.io/websocket#Conn.Ping) API
- [RFC 7692](https://tools.ietf.org/html/rfc7692) permessage-deflate compression
- [CloseRead](https://pkg.go.dev/nhooyr.io/websocket#Conn.CloseRead) helper for write only connections
- Compile to [Wasm](https://pkg.go.dev/nhooyr.io/websocket#hdr-Wasm)

## Roadmap

- [ ] Ping pong heartbeat helper [#267](https://github.com/nhooyr/websocket/issues/267)
- [ ] Graceful shutdown helper [#209](https://github.com/nhooyr/websocket/issues/209)
- [ ] Assembly for WebSocket masking [#16](https://github.com/nhooyr/websocket/issues/16)
- [ ] HTTP/2 [#4](https://github.com/nhooyr/websocket/issues/4)
- [ ] The holy grail [#402](https://github.com/nhooyr/websocket/issues/402)

## Examples

Expand All @@ -51,7 +50,7 @@ http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
if err != nil {
// ...
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
defer cancel()
Expand All @@ -78,7 +77,7 @@ c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
// ...
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

err = wsjson.Write(ctx, c, "hi")
if err != nil {
Expand Down Expand Up @@ -110,12 +109,14 @@ Advantages of nhooyr.io/websocket:
- Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
- Concurrent writes
- Close handshake ([gorilla/websocket#448](https://github.com/gorilla/websocket/issues/448))
- [CloseRead](https://pkg.go.dev/nhooyr.io/websocket#Conn.CloseRead) helper for write only connections
- Idiomatic [ping pong](https://pkg.go.dev/nhooyr.io/websocket#Conn.Ping) API
- Gorilla requires registering a pong callback before sending a Ping
- Can target Wasm ([gorilla/websocket#432](https://github.com/gorilla/websocket/issues/432))
- Transparent message buffer reuse with [wsjson](https://pkg.go.dev/nhooyr.io/websocket/wsjson) and [wspb](https://pkg.go.dev/nhooyr.io/websocket/wspb) subpackages
- Transparent message buffer reuse with [wsjson](https://pkg.go.dev/nhooyr.io/websocket/wsjson) subpackage
- [1.75x](https://github.com/nhooyr/websocket/releases/tag/v1.7.4) faster WebSocket masking implementation in pure Go
- Gorilla's implementation is slower and uses [unsafe](https://golang.org/pkg/unsafe/).
Soon we'll have assembly and be 4.5x faster [#326](https://github.com/nhooyr/websocket/pull/326)
- Full [permessage-deflate](https://tools.ietf.org/html/rfc7692) compression extension support
- Gorilla only supports no context takeover mode
- [CloseRead](https://pkg.go.dev/nhooyr.io/websocket#Conn.CloseRead) helper ([gorilla/websocket#492](https://github.com/gorilla/websocket/issues/492))
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Expand Up @@ -13,7 +13,7 @@
//
// The examples are the best way to understand how to correctly use the library.
//
// The wsjson and wspb subpackages contain helpers for JSON and protobuf messages.
// The wsjson subpackage contain helpers for JSON and protobuf messages.
//
// More documentation at https://nhooyr.io/websocket.
//
Expand Down
14 changes: 7 additions & 7 deletions example_test.go
Expand Up @@ -20,7 +20,7 @@ func ExampleAccept() {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

Check failure on line 23 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

Check failure on line 23 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
defer cancel()
Expand Down Expand Up @@ -50,7 +50,7 @@ func ExampleDial() {
if err != nil {
log.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

Check failure on line 53 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

Check failure on line 53 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

err = wsjson.Write(ctx, c, "hi")
if err != nil {
Expand All @@ -71,7 +71,7 @@ func ExampleCloseStatus() {
if err != nil {
log.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

Check failure on line 74 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

Check failure on line 74 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

_, _, err = c.Reader(ctx)
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
Expand All @@ -88,7 +88,7 @@ func Example_writeOnly() {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

Check failure on line 91 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

Check failure on line 91 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

ctx, cancel := context.WithTimeout(r.Context(), time.Minute*10)
defer cancel()
Expand Down Expand Up @@ -145,7 +145,7 @@ func ExampleConn_Ping() {
if err != nil {
log.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

Check failure on line 148 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

Check failure on line 148 in example_test.go

View workflow job for this annotation

GitHub Actions / test

c.CloseNow undefined (type *websocket.Conn has no field or method CloseNow)

// Required to read the Pongs from the server.
ctx = c.CloseRead(ctx)
Expand All @@ -162,10 +162,10 @@ func ExampleConn_Ping() {

// This example demonstrates full stack chat with an automated test.
func Example_fullStackChat() {
// https://github.com/nhooyr/websocket/tree/master/examples/chat
// https://github.com/nhooyr/websocket/tree/master/internal/examples/chat
}

// This example demonstrates a echo server.
func Example_echo() {
// https://github.com/nhooyr/websocket/tree/master/examples/echo
// https://github.com/nhooyr/websocket/tree/master/internal/examples/echo
}
2 changes: 1 addition & 1 deletion internal/examples/chat/chat.go
Expand Up @@ -74,7 +74,7 @@ func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
cs.logf("%v", err)
return
}
defer c.Close(websocket.StatusInternalError, "")
defer c.CloseNow()

err = cs.subscribe(r.Context(), c)
if errors.Is(err, context.Canceled) {
Expand Down
2 changes: 1 addition & 1 deletion internal/examples/echo/server.go
Expand Up @@ -28,7 +28,7 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.logf("%v", err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
defer c.CloseNow()

if c.Subprotocol() != "echo" {
c.Close(websocket.StatusPolicyViolation, "client must speak the echo subprotocol")
Expand Down

0 comments on commit 9d9c971

Please sign in to comment.