Skip to content

Commit b307b47

Browse files
committed
Improve docs and fix examples
Closes #207
1 parent 008b616 commit b307b47

File tree

13 files changed

+78
-69
lines changed

13 files changed

+78
-69
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ go get nhooyr.io/websocket
1515
- Minimal and idiomatic API
1616
- First class [context.Context](https://blog.golang.org/context) support
1717
- Fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite)
18-
- Thorough unit tests with [90% coverage](https://coveralls.io/github/nhooyr/websocket)
19-
- [Minimal dependencies](https://pkg.go.dev/nhooyr.io/websocket?tab=imports)
20-
- JSON and protobuf helpers in the [wsjson](https://pkg.go.dev/nhooyr.io/websocket/wsjson?tab=doc) and [wspb](https://pkg.go.dev/nhooyr.io/websocket/wspb?tab=doc) subpackages
18+
- Thorough tests with [90% coverage](https://coveralls.io/github/nhooyr/websocket)
19+
- [Zero dependencies](https://pkg.go.dev/nhooyr.io/websocket?tab=imports)
20+
- 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
2121
- Zero alloc reads and writes
2222
- Concurrent writes
2323
- [Close handshake](https://godoc.org/nhooyr.io/websocket#Conn.Close)
@@ -32,9 +32,10 @@ go get nhooyr.io/websocket
3232

3333
## Examples
3434

35-
For a production quality example that demonstrates the complete API, see the [echo example](https://godoc.org/nhooyr.io/websocket#example-package--Echo).
35+
For a production quality example that demonstrates the complete API, see the
36+
[echo example](./examples/echo).
3637

37-
For a full stack example, see [./chat-example](./chat-example).
38+
For a full stack example, see the [chat example](./examples/chat).
3839

3940
### Server
4041

@@ -98,7 +99,7 @@ Advantages of nhooyr.io/websocket:
9899
- [net.Conn](https://godoc.org/nhooyr.io/websocket#NetConn) wrapper
99100
- Zero alloc reads and writes ([gorilla/websocket#535](https://github.com/gorilla/websocket/issues/535))
100101
- Full [context.Context](https://blog.golang.org/context) support
101-
- Dials use [net/http.Client](https://golang.org/pkg/net/http/#Client)
102+
- Dial uses [net/http.Client](https://golang.org/pkg/net/http/#Client)
102103
- Will enable easy HTTP/2 support in the future
103104
- Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
104105
- Concurrent writes
@@ -111,7 +112,7 @@ Advantages of nhooyr.io/websocket:
111112
- Gorilla's implementation is slower and uses [unsafe](https://golang.org/pkg/unsafe/).
112113
- Full [permessage-deflate](https://tools.ietf.org/html/rfc7692) compression extension support
113114
- Gorilla only supports no context takeover mode
114-
- We use [klauspost/compress](https://github.com/klauspost/compress) for much lower memory usage ([gorilla/websocket#203](https://github.com/gorilla/websocket/issues/203))
115+
- We use a vendored [klauspost/compress](https://github.com/klauspost/compress) for much lower memory usage ([gorilla/websocket#203](https://github.com/gorilla/websocket/issues/203))
115116
- [CloseRead](https://godoc.org/nhooyr.io/websocket#Conn.CloseRead) helper ([gorilla/websocket#492](https://github.com/gorilla/websocket/issues/492))
116117
- Actively maintained ([gorilla/websocket#370](https://github.com/gorilla/websocket/issues/370))
117118

@@ -120,7 +121,7 @@ Advantages of nhooyr.io/websocket:
120121
[golang.org/x/net/websocket](https://godoc.org/golang.org/x/net/websocket) is deprecated.
121122
See [golang/go/issues/18152](https://github.com/golang/go/issues/18152).
122123

123-
The [net.Conn](https://godoc.org/nhooyr.io/websocket#NetConn) wrapper will ease in transitioning
124+
The [net.Conn](https://godoc.org/nhooyr.io/websocket#NetConn) can help in transitioning
124125
to nhooyr.io/websocket.
125126

126127
#### gobwas/ws

ci/test.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ gotest:
1414
go test -timeout=30m -covermode=atomic -coverprofile=ci/out/coverage.prof -coverpkg=./... $${GOTESTFLAGS-} ./...
1515
sed -i '/stringer\.go/d' ci/out/coverage.prof
1616
sed -i '/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof
17-
sed -i '/chat-example/d' ci/out/coverage.prof
17+
sed -i '/example/d' ci/out/coverage.prof

example_test.go

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import (
44
"context"
55
"log"
66
"net/http"
7-
"os"
8-
"os/signal"
97
"time"
108

119
"nhooyr.io/websocket"
1210
"nhooyr.io/websocket/wsjson"
1311
)
1412

15-
// This example accepts a WebSocket connection, reads a single JSON
16-
// message from the client and then closes the connection.
1713
func ExampleAccept() {
14+
// This handler accepts a WebSocket connection, reads a single JSON
15+
// message from the client and then closes the connection.
16+
1817
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1918
c, err := websocket.Accept(w, r, nil)
2019
if err != nil {
@@ -40,9 +39,10 @@ func ExampleAccept() {
4039
log.Fatal(err)
4140
}
4241

43-
// This example dials a server, writes a single JSON message and then
44-
// closes the connection.
4542
func ExampleDial() {
43+
// Dials a server, writes a single JSON message and then
44+
// closes the connection.
45+
4646
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
4747
defer cancel()
4848

@@ -60,9 +60,10 @@ func ExampleDial() {
6060
c.Close(websocket.StatusNormalClosure, "")
6161
}
6262

63-
// This example dials a server and then expects to be disconnected with status code
64-
// websocket.StatusNormalClosure.
6563
func ExampleCloseStatus() {
64+
// Dials a server and then expects to be disconnected with status code
65+
// websocket.StatusNormalClosure.
66+
6667
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
6768
defer cancel()
6869

@@ -78,9 +79,9 @@ func ExampleCloseStatus() {
7879
}
7980
}
8081

81-
// This example shows how to correctly handle a WebSocket connection
82-
// on which you will only write and do not expect to read data messages.
8382
func Example_writeOnly() {
83+
// This handler demonstrates how to correctly handle a write only WebSocket connection.
84+
// i.e you only expect to write messages and do not expect to read any messages.
8485
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8586
c, err := websocket.Accept(w, r, nil)
8687
if err != nil {
@@ -116,9 +117,9 @@ func Example_writeOnly() {
116117
log.Fatal(err)
117118
}
118119

119-
// This example demonstrates how to safely accept cross origin WebSockets
120-
// from the origin example.com.
121120
func Example_crossOrigin() {
121+
// This handler demonstrates how to safely accept cross origin WebSockets
122+
// from the origin example.com.
122123
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
123124
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
124125
OriginPatterns: []string{"example.com"},
@@ -141,52 +142,57 @@ func Example_crossOrigin() {
141142
// for 10 seconds.
142143
// If you CTRL+C while a connection is open, it will wait at most 30s
143144
// for all connections to terminate before shutting down.
144-
func ExampleGrace() {
145-
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
146-
c, err := websocket.Accept(w, r, nil)
147-
if err != nil {
148-
log.Println(err)
149-
return
150-
}
151-
defer c.Close(websocket.StatusInternalError, "the sky is falling")
152-
153-
ctx := c.CloseRead(r.Context())
154-
select {
155-
case <-ctx.Done():
156-
case <-time.After(time.Second * 10):
157-
}
158-
159-
c.Close(websocket.StatusNormalClosure, "")
160-
})
161-
162-
var g websocket.Grace
163-
s := &http.Server{
164-
Handler: g.Handler(fn),
165-
ReadTimeout: time.Second * 15,
166-
WriteTimeout: time.Second * 15,
167-
}
168-
169-
errc := make(chan error, 1)
170-
go func() {
171-
errc <- s.ListenAndServe()
172-
}()
173-
174-
sigs := make(chan os.Signal, 1)
175-
signal.Notify(sigs, os.Interrupt)
176-
select {
177-
case err := <-errc:
178-
log.Printf("failed to listen and serve: %v", err)
179-
case sig := <-sigs:
180-
log.Printf("terminating: %v", sig)
181-
}
182-
183-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
184-
defer cancel()
185-
s.Shutdown(ctx)
186-
g.Shutdown(ctx)
187-
}
145+
// func ExampleGrace() {
146+
// fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
147+
// c, err := websocket.Accept(w, r, nil)
148+
// if err != nil {
149+
// log.Println(err)
150+
// return
151+
// }
152+
// defer c.Close(websocket.StatusInternalError, "the sky is falling")
153+
//
154+
// ctx := c.CloseRead(r.Context())
155+
// select {
156+
// case <-ctx.Done():
157+
// case <-time.After(time.Second * 10):
158+
// }
159+
//
160+
// c.Close(websocket.StatusNormalClosure, "")
161+
// })
162+
//
163+
// var g websocket.Grace
164+
// s := &http.Server{
165+
// Handler: g.Handler(fn),
166+
// ReadTimeout: time.Second * 15,
167+
// WriteTimeout: time.Second * 15,
168+
// }
169+
//
170+
// errc := make(chan error, 1)
171+
// go func() {
172+
// errc <- s.ListenAndServe()
173+
// }()
174+
//
175+
// sigs := make(chan os.Signal, 1)
176+
// signal.Notify(sigs, os.Interrupt)
177+
// select {
178+
// case err := <-errc:
179+
// log.Printf("failed to listen and serve: %v", err)
180+
// case sig := <-sigs:
181+
// log.Printf("terminating: %v", sig)
182+
// }
183+
//
184+
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
185+
// defer cancel()
186+
// s.Shutdown(ctx)
187+
// g.Shutdown(ctx)
188+
// }
188189

189190
// This example demonstrates full stack chat with an automated test.
190191
func Example_fullStackChat() {
191-
// https://github.com/nhooyr/websocket/tree/master/chat-example
192+
// https://github.com/nhooyr/websocket/tree/master/examples/chat
193+
}
194+
195+
// This example demonstrates a echo server.
196+
func Example_echo() {
197+
// https://github.com/nhooyr/websocket/tree/master/examples/echo
192198
}

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Examples
2+
3+
This directory contains more involved examples unsuitable
4+
for display with godoc.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)