Skip to content

Commit 0d59d7f

Browse files
committed
Simplify the examples
1 parent d83ea55 commit 0d59d7f

File tree

5 files changed

+23
-38
lines changed

5 files changed

+23
-38
lines changed

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ go get nhooyr.io/websocket
3636

3737
```go
3838
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39-
c, err := websocket.Accept(w, r,
40-
websocket.AcceptSubprotocols("test"),
41-
)
39+
c, err := websocket.Accept(w, r)
4240
if err != nil {
4341
log.Printf("server handshake failed: %v", err)
4442
return
@@ -76,13 +74,10 @@ For a production quality example that shows off the low level API, see the [echo
7674
### Client
7775

7876
```go
79-
ctx := context.Background()
80-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
77+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
8178
defer cancel()
8279

83-
c, _, err := websocket.Dial(ctx, "ws://localhost:8080",
84-
websocket.DialSubprotocols("test"),
85-
)
80+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080")
8681
if err != nil {
8782
log.Fatalf("failed to ws dial: %v", err)
8883
}

accept.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ type acceptSubprotocols []string
2222

2323
func (o acceptSubprotocols) acceptOption() {}
2424

25-
// AcceptSubprotocols list the subprotocols that Accept will negotiate with a client.
26-
// The first protocol that a client supports will be negotiated.
25+
// AcceptProtocols lists the websocket protocols that Accept will negotiate with a client.
2726
// The empty protocol will always be negotiated as per RFC 6455. If you would like to
28-
// reject it, close the connection is c.Subprotocol() == "".
29-
func AcceptSubprotocols(subprotocols ...string) AcceptOption {
27+
// reject it, close the connection if c.Subprotocol() == "".
28+
func AcceptProtocols(subprotocols ...string) AcceptOption {
3029
return acceptSubprotocols(subprotocols)
3130
}
3231

example_test.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@ import (
1414

1515
func ExampleAccept_echo() {
1616
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17-
c, err := websocket.Accept(w, r,
18-
websocket.AcceptSubprotocols("echo"),
19-
)
17+
c, err := websocket.Accept(w, r)
2018
if err != nil {
2119
log.Printf("server handshake failed: %v", err)
2220
return
2321
}
2422
defer c.Close(websocket.StatusInternalError, "")
2523

26-
ctx := context.Background()
27-
2824
echo := func() error {
29-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
25+
ctx, cancel := context.WithTimeout(r.Context(), time.Minute)
3026
defer cancel()
3127

3228
typ, r, err := c.Read(ctx)
@@ -70,9 +66,7 @@ func ExampleAccept_echo() {
7066

7167
func ExampleAccept() {
7268
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73-
c, err := websocket.Accept(w, r,
74-
websocket.AcceptSubprotocols("test"),
75-
)
69+
c, err := websocket.Accept(w, r)
7670
if err != nil {
7771
log.Printf("server handshake failed: %v", err)
7872
return
@@ -106,13 +100,10 @@ func ExampleAccept() {
106100
}
107101

108102
func ExampleDial() {
109-
ctx := context.Background()
110-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
103+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
111104
defer cancel()
112105

113-
c, _, err := websocket.Dial(ctx, "ws://localhost:8080",
114-
websocket.DialSubprotocols("test"),
115-
)
106+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080")
116107
if err != nil {
117108
log.Fatalf("failed to ws dial: %v", err)
118109
}

websocket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *Conn) close(err error) {
7070

7171
// Subprotocol returns the negotiated subprotocol.
7272
// An empty string means the default protocol.
73-
func (c *Conn) Subprotocol() string {
73+
func (c *Conn) Protocol() string {
7474
return c.subprotocol
7575
}
7676

websocket_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestHandshake(t *testing.T) {
3434
{
3535
name: "handshake",
3636
server: func(w http.ResponseWriter, r *http.Request) error {
37-
c, err := websocket.Accept(w, r, websocket.AcceptSubprotocols("myproto"))
37+
c, err := websocket.Accept(w, r, websocket.AcceptProtocols("myproto"))
3838
if err != nil {
3939
return err
4040
}
@@ -74,8 +74,8 @@ func TestHandshake(t *testing.T) {
7474
}
7575
defer c.Close(websocket.StatusInternalError, "")
7676

77-
if c.Subprotocol() != "" {
78-
return xerrors.Errorf("unexpected subprotocol: %v", c.Subprotocol())
77+
if c.Protocol() != "" {
78+
return xerrors.Errorf("unexpected subprotocol: %v", c.Protocol())
7979
}
8080
return nil
8181
},
@@ -86,23 +86,23 @@ func TestHandshake(t *testing.T) {
8686
}
8787
defer c.Close(websocket.StatusInternalError, "")
8888

89-
if c.Subprotocol() != "" {
90-
return xerrors.Errorf("unexpected subprotocol: %v", c.Subprotocol())
89+
if c.Protocol() != "" {
90+
return xerrors.Errorf("unexpected subprotocol: %v", c.Protocol())
9191
}
9292
return nil
9393
},
9494
},
9595
{
9696
name: "subprotocol",
9797
server: func(w http.ResponseWriter, r *http.Request) error {
98-
c, err := websocket.Accept(w, r, websocket.AcceptSubprotocols("echo", "lar"))
98+
c, err := websocket.Accept(w, r, websocket.AcceptProtocols("echo", "lar"))
9999
if err != nil {
100100
return err
101101
}
102102
defer c.Close(websocket.StatusInternalError, "")
103103

104-
if c.Subprotocol() != "echo" {
105-
return xerrors.Errorf("unexpected subprotocol: %q", c.Subprotocol())
104+
if c.Protocol() != "echo" {
105+
return xerrors.Errorf("unexpected subprotocol: %q", c.Protocol())
106106
}
107107
return nil
108108
},
@@ -113,8 +113,8 @@ func TestHandshake(t *testing.T) {
113113
}
114114
defer c.Close(websocket.StatusInternalError, "")
115115

116-
if c.Subprotocol() != "echo" {
117-
return xerrors.Errorf("unexpected subprotocol: %q", c.Subprotocol())
116+
if c.Protocol() != "echo" {
117+
return xerrors.Errorf("unexpected subprotocol: %q", c.Protocol())
118118
}
119119
return nil
120120
},
@@ -266,7 +266,7 @@ func TestAutobahnServer(t *testing.T) {
266266

267267
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
268268
c, err := websocket.Accept(w, r,
269-
websocket.AcceptSubprotocols("echo"),
269+
websocket.AcceptProtocols("echo"),
270270
)
271271
if err != nil {
272272
t.Logf("server handshake failed: %+v", err)

0 commit comments

Comments
 (0)