Skip to content

Commit 9f166cc

Browse files
committed
Improve docs and add more examples
1 parent c03a94a commit 9f166cc

File tree

9 files changed

+89
-45
lines changed

9 files changed

+89
-45
lines changed

README.md

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,32 @@ go get nhooyr.io/websocket
3131
- [ ] WebSockets over HTTP/2 [#4](https://github.com/nhooyr/websocket/issues/4)
3232
- [ ] Deflate extension support [#5](https://github.com/nhooyr/websocket/issues/5)
3333

34-
## Example
34+
## Examples
3535

3636
For a production quality example that shows off the full API, see the [echo example on the godoc](https://godoc.org/nhooyr.io/websocket#example-package--Echo).
3737

3838
### Server
3939

4040
```go
41-
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
41+
http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
4242
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
4343
if err != nil {
4444
// ...
4545
}
46-
defer c.Close(websocket.StatusInternalError, "")
46+
defer c.Close(websocket.StatusInternalError, "the sky is falling")
4747

4848
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
4949
defer cancel()
50-
51-
_, r, err := c.Reader(ctx)
52-
if err != nil {
53-
// ...
54-
}
5550

56-
b, err := ioutil.ReadAll(r)
51+
var v interface{}
52+
err = wsjson.Read(ctx, c, &v)
5753
if err != nil {
5854
// ...
5955
}
6056

61-
fmt.Printf("received %q\n", b)
57+
log.Printf("received: %v", v)
6258

63-
c.Close(websocket.StatusNormalClosure, "")
59+
c.Close(websocket.StatusNormalClosure, "success")
6460
})
6561
```
6662

@@ -71,27 +67,17 @@ ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
7167
defer cancel()
7268

7369
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", websocket.DialOptions{})
74-
if err != nil {
75-
return err
76-
}
77-
defer c.Close(websocket.StatusInternalError, "")
78-
79-
ww, err := c.Writer(ctx)
80-
if err != nil {
81-
// ...
82-
}
83-
84-
_, err = w.Write([]byte("hi"))
8570
if err != nil {
8671
// ...
8772
}
73+
defer c.Close(websocket.StatusInternalError, "the sky is falling")
8874

89-
err = w.Close()
75+
err = wsjson.Write(ctx, c, "hi")
9076
if err != nil {
9177
// ...
9278
}
9379

94-
c.Close(websocket.StatusNormalClosure, "")
80+
c.Close(websocket.StatusNormalClosure, "done")
9581
```
9682

9783
## Design considerations

ci/bench/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM golang:1.12
22

33
LABEL "com.github.actions.name"="bench"
4-
LABEL "com.github.actions.description"="bench"
4+
LABEL "com.github.actions.description"=""
55
LABEL "com.github.actions.icon"="code"
6-
LABEL "com.github.actions.color"="purple"
6+
LABEL "com.github.actions.color"="red"
77

88
COPY entrypoint.sh /entrypoint.sh
99

ci/fmt/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM golang:1.12
22

33
LABEL "com.github.actions.name"="fmt"
4-
LABEL "com.github.actions.description"="fmt"
4+
LABEL "com.github.actions.description"=""
55
LABEL "com.github.actions.icon"="code"
6-
LABEL "com.github.actions.color"="purple"
6+
LABEL "com.github.actions.color"="blue"
77

88
COPY entrypoint.sh /entrypoint.sh
99

ci/lint/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM codercom/playcicache
22

33
LABEL "com.github.actions.name"="lint"
4-
LABEL "com.github.actions.description"="lint"
4+
LABEL "com.github.actions.description"=""
55
LABEL "com.github.actions.icon"="code"
6-
LABEL "com.github.actions.color"="purple"
6+
LABEL "com.github.actions.color"="pink"
77

88
COPY entrypoint.sh /entrypoint.sh
99

ci/test/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
FROM golang:1.12
22

33
LABEL "com.github.actions.name"="test"
4-
LABEL "com.github.actions.description"="test"
4+
LABEL "com.github.actions.description"=""
55
LABEL "com.github.actions.icon"="code"
6-
LABEL "com.github.actions.color"="purple"
6+
LABEL "com.github.actions.color"="green"
77

88
RUN apt update && \
99
apt install -y shellcheck python-pip && \

doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// a WebSocket server, Accept to accept a WebSocket client dial and then Conn to interact
1010
// with the resulting WebSocket connections.
1111
//
12-
// The echo example is the best way to understand how to correctly use the library.
12+
// The examples are the best way to understand how to correctly use the library.
1313
//
14-
// The wsjson and wspb packages contain helpers for JSON and ProtoBuf messages.
14+
// The wsjson and wspb subpackages contain helpers for JSON and ProtoBuf messages.
1515
package websocket

example_echo_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ func Example_echo() {
5050
}
5151
}()
5252

53-
// Now we dial the server and send the messages.
53+
// Now we dial the server, send the messages and echo the responses.
5454
err = client("ws://" + l.Addr().String())
5555
if err != nil {
5656
log.Fatalf("client failed: %v", err)
5757
}
5858

5959
// Output:
60-
// 0
61-
// 1
62-
// 2
63-
// 3
64-
// 4
60+
// received: map[i:0]
61+
// received: map[i:1]
62+
// received: map[i:2]
63+
// received: map[i:3]
64+
// received: map[i:4]
6565
}
6666

6767
// echoServer is the WebSocket echo server implementation.
@@ -74,7 +74,7 @@ func echoServer(w http.ResponseWriter, r *http.Request) error {
7474
if err != nil {
7575
return err
7676
}
77-
defer c.Close(websocket.StatusInternalError, "")
77+
defer c.Close(websocket.StatusInternalError, "the sky is falling")
7878

7979
if c.Subprotocol() == "" {
8080
c.Close(websocket.StatusPolicyViolation, "cannot communicate with the default protocol")
@@ -138,7 +138,7 @@ func client(url string) error {
138138
if err != nil {
139139
return err
140140
}
141-
defer c.Close(websocket.StatusInternalError, "")
141+
defer c.Close(websocket.StatusInternalError, "the sky is falling")
142142

143143
for i := 0; i < 5; i++ {
144144
err = wsjson.Write(ctx, c, map[string]int{
@@ -154,9 +154,9 @@ func client(url string) error {
154154
return err
155155
}
156156

157-
fmt.Printf("%v\n", v["i"])
157+
fmt.Printf("received: %v\n", v)
158158
}
159159

160-
c.Close(websocket.StatusNormalClosure, "")
160+
c.Close(websocket.StatusNormalClosure, "done")
161161
return nil
162162
}

example_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package websocket_test
2+
3+
import (
4+
"context"
5+
"log"
6+
"net/http"
7+
"time"
8+
9+
"nhooyr.io/websocket"
10+
"nhooyr.io/websocket/wsjson"
11+
)
12+
13+
func ExampleAccept() {
14+
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15+
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
16+
if err != nil {
17+
log.Println(err)
18+
return
19+
}
20+
defer c.Close(websocket.StatusInternalError, "the sky is falling")
21+
22+
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
23+
defer cancel()
24+
25+
var v interface{}
26+
err = wsjson.Read(ctx, c, &v)
27+
if err != nil {
28+
log.Println(err)
29+
return
30+
}
31+
32+
log.Printf("received: %v", v)
33+
34+
c.Close(websocket.StatusNormalClosure, "success")
35+
})
36+
37+
http.ListenAndServe("localhost:8080", fn)
38+
}
39+
40+
func ExampleDial() {
41+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
42+
defer cancel()
43+
44+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", websocket.DialOptions{})
45+
if err != nil {
46+
log.Println(err)
47+
return
48+
}
49+
defer c.Close(websocket.StatusInternalError, "the sky is falling")
50+
51+
err = wsjson.Write(ctx, c, "hi")
52+
if err != nil {
53+
log.Println(err)
54+
return
55+
}
56+
57+
c.Close(websocket.StatusNormalClosure, "done")
58+
}

statuscode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242
statusTLSHandshake
4343
)
4444

45-
// CloseError represents an error from a WebSocket close frame.
45+
// CloseError represents a WebSocket close frame.
4646
// It is returned by Conn's methods when the Connection is closed with a WebSocket close frame.
4747
type CloseError struct {
4848
Code StatusCode

0 commit comments

Comments
 (0)