Skip to content

Commit b4ce02e

Browse files
authored
Merge pull request #37 from nhooyr/name-change
Rename ws to websocket and clean up API
2 parents 53e972a + b9de5c8 commit b4ce02e

19 files changed

+103
-108
lines changed

README.md

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# ws
1+
# websocket
22

3-
[![GoDoc](https://godoc.org/nhooyr.io/ws?status.svg)](https://godoc.org/nhooyr.io/ws)
4-
[![Codecov](https://img.shields.io/codecov/c/github/nhooyr/ws.svg)](https://codecov.io/gh/nhooyr/ws)
5-
[![GitHub release](https://img.shields.io/github/release/nhooyr/ws.svg)](https://github.com/nhooyr/ws/releases)
3+
[![GoDoc](https://godoc.org/nhooyr.io/websocket?status.svg)](https://godoc.org/nhooyr.io/websocket)
4+
[![Codecov](https://img.shields.io/codecov/c/github/nhooyr/websocket.svg)](https://codecov.io/gh/nhooyr/websocket)
5+
[![GitHub release](https://img.shields.io/github/release/nhooyr/websocket.svg)](https://github.com/nhooyr/websocket/releases)
66

7-
ws is a minimal and idiomatic WebSocket library for Go.
7+
websocket is a minimal and idiomatic WebSocket library for Go.
88

99
This library is in heavy development.
1010

1111
## Install
1212

1313
```bash
14-
go get nhooyr.io/ws@master
14+
go get nhooyr.io/websocket@master
1515
```
1616

1717
## Features
@@ -20,10 +20,9 @@ go get nhooyr.io/ws@master
2020
- Simple to use because of the minimal API
2121
- Uses the context package for cancellation
2222
- Uses net/http's Client to do WebSocket dials
23-
- JSON and Protobuf helpers in wsjson and wspb subpackages
24-
- Compression extension is supported
23+
- Compression of text frames larger than 1024 bytes by default
2524
- Highly optimized
26-
- API will be ready for WebSockets over HTTP/2
25+
- API will transparently work with WebSockets over HTTP/2
2726
- WASM support
2827

2928
## Example
@@ -33,57 +32,65 @@ go get nhooyr.io/ws@master
3332
```go
3433
func main() {
3534
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36-
c, err := ws.Accept(w, r)
35+
c, err := websocket.Accept(w, r,
36+
websocket.AcceptSubprotocols("test"),
37+
)
3738
if err != nil {
3839
log.Printf("server handshake failed: %v", err)
3940
return
4041
}
41-
defer c.Close(ws.StatusInternalError, "")
42+
defer c.Close(websocket.StatusInternalError, "")
4243

4344
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
4445
defer cancel()
4546

46-
err = wsjson.Write(ctx, c, map[string]interface{}{
47+
v := map[string]interface{}{
4748
"my_field": "foo",
48-
})
49+
}
50+
err = websocket.WriteJSON(ctx, c, v)
4951
if err != nil {
50-
log.Printf("failed to write json struct: %v", err)
52+
log.Printf("failed to write json: %v", err)
5153
return
5254
}
5355

54-
c.Close(ws.StatusNormalClosure, "")
56+
log.Printf("wrote %v", v)
57+
58+
c.Close(websocket.StatusNormalClosure, "")
5559
})
56-
// For production deployments, use a net/http.Server configured
57-
// with the appropriate timeouts.
5860
err := http.ListenAndServe("localhost:8080", fn)
5961
if err != nil {
6062
log.Fatalf("failed to listen and serve: %v", err)
6163
}
6264
}
6365
```
6466

67+
For a production quality example that shows off the low level API, see the [echo example](https://godoc.org/nhooyr.io/websocket#ex-Accept--Echo).
68+
6569
### Client
6670

6771
```go
6872
func main() {
6973
ctx := context.Background()
70-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
74+
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
7175
defer cancel()
7276

73-
c, _, err := ws.Dial(ctx, "ws://localhost:8080")
77+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080",
78+
websocket.DialSubprotocols("test"),
79+
)
7480
if err != nil {
7581
log.Fatalf("failed to ws dial: %v", err)
7682
}
77-
defer c.Close(ws.StatusInternalError, "")
83+
defer c.Close(websocket.StatusInternalError, "")
7884

79-
err = wsjson.Write(ctx, c, map[string]interface{}{
80-
"my_field": "foo",
81-
})
85+
var v interface{}
86+
err = websocket.ReadJSON(ctx, c, v)
8287
if err != nil {
83-
log.Fatalf("failed to write json struct: %v", err)
88+
log.Fatalf("failed to read json: %v", err)
8489
}
8590

86-
c.Close(ws.StatusNormalClosure, "")
91+
log.Printf("received %v", v)
92+
93+
c.Close(websocket.StatusNormalClosure, "")
8794
}
8895
```
8996

@@ -111,10 +118,10 @@ https://github.com/gorilla/websocket
111118
This package is the community standard but it is very old and over timennn
112119
has accumulated cruft. There are many ways to do the same thing and the API
113120
overall is just not very clear. Just compare the godoc of
114-
[nhooyr/ws](godoc.org/github.com/nhooyr/ws) side by side with
121+
[nhooyr/websocket](godoc.org/github.com/nhooyr/websocket) side by side with
115122
[gorilla/websocket](godoc.org/github.com/gorilla/websocket).
116123

117-
The API for nhooyr/ws has been designed such that there is only one way to do things
124+
The API for nhooyr/websocket has been designed such that there is only one way to do things
118125
and with HTTP/2 in mind which makes using it correctly and safely much easier.
119126

120127
### x/net/websocket
@@ -134,8 +141,8 @@ and clarity. Its just not clear how to do things in a safe manner.
134141

135142
This library is fantastic in terms of performance though. The author put in significant
136143
effort to ensure its speed and I have tried to apply as many of its teachings as
137-
I could into nhooyr/ws.
144+
I could into nhooyr/websocket.
138145

139146
If you want a library that gives you absolute control over everything, this is the library,
140-
but for most users, the API provided by nhooyr/ws will definitely fit better as it will
147+
but for most users, the API provided by nhooyr/websocket will definitely fit better as it will
141148
be just as performant but much easier to use.

accept.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ws
1+
package websocket
22

33
import (
44
"fmt"
@@ -21,10 +21,11 @@ func AcceptSubprotocols(subprotocols ...string) AcceptOption {
2121
// AcceptOrigins lists the origins that Accept will accept.
2222
// Accept will always accept r.Host as the origin so you do not need to
2323
// specify that with this option.
24+
//
2425
// Use this option with caution to avoid exposing your WebSocket
2526
// server to a CSRF attack.
2627
// See https://stackoverflow.com/a/37837709/4283659
27-
// You can use a * to specify wildcards in domain names.
28+
// You can use a * to specify wildcards.
2829
func AcceptOrigins(origins ...string) AcceptOption {
2930
panic("TODO")
3031
}
@@ -33,6 +34,7 @@ func AcceptOrigins(origins ...string) AcceptOption {
3334
// the connection to WebSocket.
3435
// Accept will reject the handshake if the Origin is not the same as the Host unless
3536
// InsecureAcceptOrigin is passed.
37+
// Accept uses w to write the handshake response so the timeouts on the http.Server apply.
3638
func Accept(w http.ResponseWriter, r *http.Request, opts ...AcceptOption) (*Conn, error) {
3739
panic("TODO")
3840
}

datatype.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ws
1+
package websocket
22

33
// DataType represents the Opcode of a WebSocket data frame.
44
//go:generate stringer -type=DataType

datatype_string.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dial.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ws
1+
package websocket
22

33
import (
44
"context"

doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package ws is a minimal and idiomatic implementation of the WebSocket protocol.
1+
// Package websocket is a minimal and idiomatic implementation of the WebSocket protocol.
22
//
3-
// For now the docs are at https://github.com/nhooyr/ws#ws. I will move them here later.
4-
package ws
3+
// For now the docs are at https://github.com/nhooyr/websocket#websocket. I will move them here later.
4+
package websocket

example_test.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ws_test
1+
package websocket_test
22

33
import (
44
"context"
@@ -9,20 +9,19 @@ import (
99

1010
"golang.org/x/time/rate"
1111

12-
"nhooyr.io/ws"
13-
"nhooyr.io/ws/wsjson"
12+
"nhooyr.io/websocket"
1413
)
1514

1615
func ExampleAccept_echo() {
1716
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18-
c, err := ws.Accept(w, r,
19-
ws.AcceptSubprotocols("echo"),
17+
c, err := websocket.Accept(w, r,
18+
websocket.AcceptSubprotocols("echo"),
2019
)
2120
if err != nil {
2221
log.Printf("server handshake failed: %v", err)
2322
return
2423
}
25-
defer c.Close(ws.StatusInternalError, "")
24+
defer c.Close(websocket.StatusInternalError, "")
2625

2726
ctx := context.Background()
2827

@@ -65,38 +64,46 @@ func ExampleAccept_echo() {
6564
}
6665
}
6766
})
68-
// For production deployments, use a net/http.Server configured
69-
// with the appropriate timeouts.
70-
err := http.ListenAndServe("localhost:8080", fn)
67+
68+
s := &http.Server{
69+
Addr: "localhost:8080",
70+
Handler: fn,
71+
ReadTimeout: time.Second * 15,
72+
WriteTimeout: time.Second * 15,
73+
}
74+
err := s.ListenAndServe()
7175
if err != nil {
7276
log.Fatalf("failed to listen and serve: %v", err)
7377
}
7478
}
7579

7680
func ExampleAccept() {
7781
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
78-
c, err := ws.Accept(w, r)
82+
c, err := websocket.Accept(w, r,
83+
websocket.AcceptSubprotocols("test"),
84+
)
7985
if err != nil {
8086
log.Printf("server handshake failed: %v", err)
8187
return
8288
}
83-
defer c.Close(ws.StatusInternalError, "")
89+
defer c.Close(websocket.StatusInternalError, "")
8490

8591
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
8692
defer cancel()
8793

88-
err = wsjson.Write(ctx, c, map[string]interface{}{
94+
v := map[string]interface{}{
8995
"my_field": "foo",
90-
})
96+
}
97+
err = websocket.WriteJSON(ctx, c, v)
9198
if err != nil {
92-
log.Printf("failed to write json struct: %v", err)
99+
log.Printf("failed to write json: %v", err)
93100
return
94101
}
95102

96-
c.Close(ws.StatusNormalClosure, "")
103+
log.Printf("wrote %v", v)
104+
105+
c.Close(websocket.StatusNormalClosure, "")
97106
})
98-
// For production deployments, use a net/http.Server configured
99-
// with the appropriate timeouts.
100107
err := http.ListenAndServe("localhost:8080", fn)
101108
if err != nil {
102109
log.Fatalf("failed to listen and serve: %v", err)
@@ -108,18 +115,21 @@ func ExampleDial() {
108115
ctx, cancel := context.WithTimeout(ctx, time.Minute)
109116
defer cancel()
110117

111-
c, _, err := ws.Dial(ctx, "ws://localhost:8080")
118+
c, _, err := websocket.Dial(ctx, "ws://localhost:8080",
119+
websocket.DialSubprotocols("test"),
120+
)
112121
if err != nil {
113122
log.Fatalf("failed to ws dial: %v", err)
114123
}
115-
defer c.Close(ws.StatusInternalError, "")
124+
defer c.Close(websocket.StatusInternalError, "")
116125

117-
err = wsjson.Write(ctx, c, map[string]interface{}{
118-
"my_field": "foo",
119-
})
126+
var v interface{}
127+
err = websocket.ReadJSON(ctx, c, v)
120128
if err != nil {
121-
log.Fatalf("failed to write json struct: %v", err)
129+
log.Fatalf("failed to read json: %v", err)
122130
}
123131

124-
c.Close(ws.StatusNormalClosure, "")
132+
log.Printf("received %v", v)
133+
134+
c.Close(websocket.StatusNormalClosure, "")
125135
}

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
module nhooyr.io/ws
1+
module nhooyr.io/websocket
22

33
go 1.12
44

55
require (
6-
github.com/golang/protobuf v1.3.1
76
github.com/kr/pretty v0.1.0 // indirect
87
go.coder.com/go-tools v0.0.0-20190317003359-0c6a35b74a16
98
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
2-
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
31
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
42
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
53
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

header.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ws
1+
package websocket
22

33
import (
44
"io"

0 commit comments

Comments
 (0)