@@ -16,85 +16,81 @@ go get nhooyr.io/websocket@master
16
16
17
17
## Features
18
18
19
+ - HTTP/2 over WebSocket's support
19
20
- Full support of the WebSocket protocol
20
21
- Only depends on the stdlib
21
22
- Simple to use because of the minimal API
22
23
- Uses the context package for cancellation
23
24
- Uses net/http's Client to do WebSocket dials
24
25
- Compression of text frames larger than 1024 bytes by default
25
- - Highly optimized
26
- - API will transparently work with WebSockets over HTTP/2
26
+ - Highly optimized where it matters
27
27
- WASM support
28
28
29
29
## Example
30
30
31
31
### Server
32
32
33
33
``` go
34
- func main () {
35
- fn := http.HandlerFunc (func (w http.ResponseWriter , r *http.Request ) {
36
- c , err := websocket.Accept (w, r,
37
- websocket.AcceptSubprotocols (" test" ),
38
- )
39
- if err != nil {
40
- log.Printf (" server handshake failed: %v " , err)
41
- return
42
- }
43
- defer c.Close (websocket.StatusInternalError , " " )
44
-
45
- ctx , cancel := context.WithTimeout (r.Context (), time.Second *10 )
46
- defer cancel ()
47
-
48
- v := map [string ]interface {}{
49
- " my_field" : " foo" ,
50
- }
51
- err = websocket.WriteJSON (ctx, c, v)
52
- if err != nil {
53
- log.Printf (" failed to write json: %v " , err)
54
- return
55
- }
56
-
57
- log.Printf (" wrote %v " , v)
58
-
59
- c.Close (websocket.StatusNormalClosure , " " )
60
- })
61
- err := http.ListenAndServe (" localhost:8080" , fn)
34
+ fn := http.HandlerFunc (func (w http.ResponseWriter , r *http.Request ) {
35
+ c , err := websocket.Accept (w, r,
36
+ websocket.AcceptSubprotocols (" test" ),
37
+ )
62
38
if err != nil {
63
- log.Fatalf (" failed to listen and serve: %v " , err)
39
+ log.Printf (" server handshake failed: %v " , err)
40
+ return
64
41
}
65
- }
66
- ```
67
-
68
- For a production quality example that shows off the low level API, see the echo example on the [ godoc] ( https://godoc.org/nhooyr.io/websocket#Accept ) .
69
-
70
- ### Client
42
+ defer c.Close (websocket.StatusInternalError , " " )
71
43
72
- ``` go
73
- func main () {
74
- ctx := context.Background ()
75
- ctx , cancel := context.WithTimeout (ctx, time.Second *10 )
44
+ ctx , cancel := context.WithTimeout (r.Context (), time.Second *10 )
76
45
defer cancel ()
77
46
78
- c , _ , err := websocket.Dial (ctx, " ws://localhost:8080" ,
79
- websocket.DialSubprotocols (" test" ),
80
- )
81
- if err != nil {
82
- log.Fatalf (" failed to ws dial: %v " , err)
47
+ v := map [string ]interface {}{
48
+ " my_field" : " foo" ,
83
49
}
84
- defer c.Close (websocket.StatusInternalError , " " )
85
-
86
- var v interface {}
87
- err = websocket.ReadJSON (ctx, c, v)
50
+ err = websocket.WriteJSON (ctx, c, v)
88
51
if err != nil {
89
- log.Fatalf (" failed to read json: %v " , err)
52
+ log.Printf (" failed to write json: %v " , err)
53
+ return
90
54
}
91
55
92
- log.Printf (" received %v " , v)
56
+ log.Printf (" wrote %v " , v)
93
57
94
58
c.Close (websocket.StatusNormalClosure , " " )
59
+ })
60
+ err := http.ListenAndServe (" localhost:8080" , fn)
61
+ if err != nil {
62
+ log.Fatalf (" failed to listen and serve: %v " , err)
95
63
}
96
64
```
97
65
66
+ For a production quality example that shows off the low level API, see the echo example on the [ godoc] ( https://godoc.org/nhooyr.io/websocket#Accept ) .
67
+
68
+ ### Client
69
+
70
+ ``` go
71
+ ctx := context.Background ()
72
+ ctx , cancel := context.WithTimeout (ctx, time.Second *10 )
73
+ defer cancel ()
74
+
75
+ c , _ , err := websocket.Dial (ctx, " ws://localhost:8080" ,
76
+ websocket.DialSubprotocols (" test" ),
77
+ )
78
+ if err != nil {
79
+ log.Fatalf (" failed to ws dial: %v " , err)
80
+ }
81
+ defer c.Close (websocket.StatusInternalError , " " )
82
+
83
+ var v interface {}
84
+ err = websocket.ReadJSON (ctx, c, v)
85
+ if err != nil {
86
+ log.Fatalf (" failed to read json: %v " , err)
87
+ }
88
+
89
+ log.Printf (" received %v " , v)
90
+
91
+ c.Close (websocket.StatusNormalClosure , " " )
92
+ ```
93
+
98
94
See [ example_test.go] ( example_test.go ) for more examples.
99
95
100
96
## Design considerations
@@ -106,7 +102,7 @@ See [example_test.go](example_test.go) for more examples.
106
102
- net.Conn is never exposed as WebSocket's over HTTP/2 will not have a net.Conn.
107
103
- Functional options make the API very clean and easy to extend
108
104
- Compression is very useful for JSON payloads
109
- - Protobuf and JSON helpers make code terse
105
+ - JSON helpers make code terse
110
106
- Using net/http's Client for dialing means we do not have to reinvent dialing hooks
111
107
and configurations. Just pass in a custom net/http client if you want custom dialing.
112
108
@@ -120,14 +116,14 @@ WebSocket protocol correctly so big thanks to the authors of both.
120
116
121
117
https://github.com/gorilla/websocket
122
118
123
- This package is the community standard but it is very old and over timennn
119
+ This package is the community standard but it is very old and over time
124
120
has accumulated cruft. There are many ways to do the same thing and the API
125
- overall is just not very clear. Just compare the godoc of
121
+ is not clear. Just compare the godoc of
126
122
[ nhooyr/websocket] ( godoc.org/github.com/nhooyr/websocket ) side by side with
127
123
[ gorilla/websocket] ( godoc.org/github.com/gorilla/websocket ) .
128
124
129
125
The API for nhooyr/websocket has been designed such that there is only one way to do things
130
- and with HTTP/2 in mind which makes using it correctly and safely much easier.
126
+ which makes using it correctly and safely much easier.
131
127
132
128
### x/net/websocket
133
129
@@ -142,12 +138,12 @@ See https://github.com/golang/go/issues/18152
142
138
https://github.com/gobwas/ws
143
139
144
140
This library has an extremely flexible API but that comes at the cost of usability
145
- and clarity. Its just not clear how to do things in a safe manner.
141
+ and clarity. Its not clear what the best way to do anything is.
146
142
147
- This library is fantastic in terms of performance though . The author put in significant
148
- effort to ensure its speed and I have tried to apply as many of its teachings as
143
+ This library is fantastic in terms of performance. The author put in significant
144
+ effort to ensure its speed and I have applied as many of its optimizations as
149
145
I could into nhooyr/websocket.
150
146
151
147
If you want a library that gives you absolute control over everything, this is the library,
152
148
but for most users, the API provided by nhooyr/websocket will definitely fit better as it will
153
- be just as performant but much easier to use.
149
+ be just as performant but much easier to use correctly .
0 commit comments