good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit 0d59d7f5 authored by Anmol Sethi's avatar Anmol Sethi
Browse files

Simplify the examples

parent d83ea559
No related branches found
No related tags found
No related merge requests found
......@@ -36,9 +36,7 @@ go get nhooyr.io/websocket
```go
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r,
websocket.AcceptSubprotocols("test"),
)
c, err := websocket.Accept(w, r)
if err != nil {
log.Printf("server handshake failed: %v", err)
return
......@@ -76,13 +74,10 @@ For a production quality example that shows off the low level API, see the [echo
### Client
```go
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, "ws://localhost:8080",
websocket.DialSubprotocols("test"),
)
c, _, err := websocket.Dial(ctx, "ws://localhost:8080")
if err != nil {
log.Fatalf("failed to ws dial: %v", err)
}
......
......@@ -22,11 +22,10 @@ type acceptSubprotocols []string
func (o acceptSubprotocols) acceptOption() {}
// AcceptSubprotocols list the subprotocols that Accept will negotiate with a client.
// The first protocol that a client supports will be negotiated.
// AcceptProtocols lists the websocket protocols that Accept will negotiate with a client.
// The empty protocol will always be negotiated as per RFC 6455. If you would like to
// reject it, close the connection is c.Subprotocol() == "".
func AcceptSubprotocols(subprotocols ...string) AcceptOption {
// reject it, close the connection if c.Subprotocol() == "".
func AcceptProtocols(subprotocols ...string) AcceptOption {
return acceptSubprotocols(subprotocols)
}
......
......@@ -14,19 +14,15 @@ import (
func ExampleAccept_echo() {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r,
websocket.AcceptSubprotocols("echo"),
)
c, err := websocket.Accept(w, r)
if err != nil {
log.Printf("server handshake failed: %v", err)
return
}
defer c.Close(websocket.StatusInternalError, "")
ctx := context.Background()
echo := func() error {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
ctx, cancel := context.WithTimeout(r.Context(), time.Minute)
defer cancel()
typ, r, err := c.Read(ctx)
......@@ -70,9 +66,7 @@ func ExampleAccept_echo() {
func ExampleAccept() {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r,
websocket.AcceptSubprotocols("test"),
)
c, err := websocket.Accept(w, r)
if err != nil {
log.Printf("server handshake failed: %v", err)
return
......@@ -106,13 +100,10 @@ func ExampleAccept() {
}
func ExampleDial() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, "ws://localhost:8080",
websocket.DialSubprotocols("test"),
)
c, _, err := websocket.Dial(ctx, "ws://localhost:8080")
if err != nil {
log.Fatalf("failed to ws dial: %v", err)
}
......
......@@ -70,7 +70,7 @@ func (c *Conn) close(err error) {
// Subprotocol returns the negotiated subprotocol.
// An empty string means the default protocol.
func (c *Conn) Subprotocol() string {
func (c *Conn) Protocol() string {
return c.subprotocol
}
......
......@@ -34,7 +34,7 @@ func TestHandshake(t *testing.T) {
{
name: "handshake",
server: func(w http.ResponseWriter, r *http.Request) error {
c, err := websocket.Accept(w, r, websocket.AcceptSubprotocols("myproto"))
c, err := websocket.Accept(w, r, websocket.AcceptProtocols("myproto"))
if err != nil {
return err
}
......@@ -74,8 +74,8 @@ func TestHandshake(t *testing.T) {
}
defer c.Close(websocket.StatusInternalError, "")
if c.Subprotocol() != "" {
return xerrors.Errorf("unexpected subprotocol: %v", c.Subprotocol())
if c.Protocol() != "" {
return xerrors.Errorf("unexpected subprotocol: %v", c.Protocol())
}
return nil
},
......@@ -86,8 +86,8 @@ func TestHandshake(t *testing.T) {
}
defer c.Close(websocket.StatusInternalError, "")
if c.Subprotocol() != "" {
return xerrors.Errorf("unexpected subprotocol: %v", c.Subprotocol())
if c.Protocol() != "" {
return xerrors.Errorf("unexpected subprotocol: %v", c.Protocol())
}
return nil
},
......@@ -95,14 +95,14 @@ func TestHandshake(t *testing.T) {
{
name: "subprotocol",
server: func(w http.ResponseWriter, r *http.Request) error {
c, err := websocket.Accept(w, r, websocket.AcceptSubprotocols("echo", "lar"))
c, err := websocket.Accept(w, r, websocket.AcceptProtocols("echo", "lar"))
if err != nil {
return err
}
defer c.Close(websocket.StatusInternalError, "")
if c.Subprotocol() != "echo" {
return xerrors.Errorf("unexpected subprotocol: %q", c.Subprotocol())
if c.Protocol() != "echo" {
return xerrors.Errorf("unexpected subprotocol: %q", c.Protocol())
}
return nil
},
......@@ -113,8 +113,8 @@ func TestHandshake(t *testing.T) {
}
defer c.Close(websocket.StatusInternalError, "")
if c.Subprotocol() != "echo" {
return xerrors.Errorf("unexpected subprotocol: %q", c.Subprotocol())
if c.Protocol() != "echo" {
return xerrors.Errorf("unexpected subprotocol: %q", c.Protocol())
}
return nil
},
......@@ -266,7 +266,7 @@ func TestAutobahnServer(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r,
websocket.AcceptSubprotocols("echo"),
websocket.AcceptProtocols("echo"),
)
if err != nil {
t.Logf("server handshake failed: %+v", err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment