From 0d59d7f5d9960e6c785fcdfb2bf66c5b901919a2 Mon Sep 17 00:00:00 2001
From: Anmol Sethi <hi@nhooyr.io>
Date: Sat, 13 Apr 2019 13:41:44 -0500
Subject: [PATCH] Simplify the examples

---
 README.md         | 11 +++--------
 accept.go         |  7 +++----
 example_test.go   | 19 +++++--------------
 websocket.go      |  2 +-
 websocket_test.go | 22 +++++++++++-----------
 5 files changed, 23 insertions(+), 38 deletions(-)

diff --git a/README.md b/README.md
index 96bffe7..71a5fd8 100644
--- a/README.md
+++ b/README.md
@@ -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)
 }
diff --git a/accept.go b/accept.go
index 63bccc4..d2ec8b2 100644
--- a/accept.go
+++ b/accept.go
@@ -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)
 }
 
diff --git a/example_test.go b/example_test.go
index c23e91a..5c2d2b2 100644
--- a/example_test.go
+++ b/example_test.go
@@ -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)
 	}
diff --git a/websocket.go b/websocket.go
index 09a94e7..4a552b5 100644
--- a/websocket.go
+++ b/websocket.go
@@ -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
 }
 
diff --git a/websocket_test.go b/websocket_test.go
index 14dcc8c..6f5869c 100644
--- a/websocket_test.go
+++ b/websocket_test.go
@@ -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)
-- 
GitLab