@@ -16,85 +16,81 @@ go get nhooyr.io/websocket@master
1616
1717## Features
1818
19+ - HTTP/2 over WebSocket's support
1920- Full support of the WebSocket protocol
2021- Only depends on the stdlib
2122- Simple to use because of the minimal API
2223- Uses the context package for cancellation
2324- Uses net/http's Client to do WebSocket dials
2425- 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
2727- WASM support
2828
2929## Example
3030
3131### Server
3232
3333``` 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+ )
6238 if err != nil {
63- log.Fatalf (" failed to listen and serve: %v " , err)
39+ log.Printf (" server handshake failed: %v " , err)
40+ return
6441 }
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 , " " )
7143
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 )
7645 defer cancel ()
7746
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" ,
8349 }
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)
8851 if err != nil {
89- log.Fatalf (" failed to read json: %v " , err)
52+ log.Printf (" failed to write json: %v " , err)
53+ return
9054 }
9155
92- log.Printf (" received %v " , v)
56+ log.Printf (" wrote %v " , v)
9357
9458 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)
9563}
9664```
9765
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+
9894See [ example_test.go] ( example_test.go ) for more examples.
9995
10096## Design considerations
@@ -106,7 +102,7 @@ See [example_test.go](example_test.go) for more examples.
106102- net.Conn is never exposed as WebSocket's over HTTP/2 will not have a net.Conn.
107103- Functional options make the API very clean and easy to extend
108104- Compression is very useful for JSON payloads
109- - Protobuf and JSON helpers make code terse
105+ - JSON helpers make code terse
110106- Using net/http's Client for dialing means we do not have to reinvent dialing hooks
111107 and configurations. Just pass in a custom net/http client if you want custom dialing.
112108
@@ -120,14 +116,14 @@ WebSocket protocol correctly so big thanks to the authors of both.
120116
121117https://github.com/gorilla/websocket
122118
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
124120has 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
126122[ nhooyr/websocket] ( godoc.org/github.com/nhooyr/websocket ) side by side with
127123[ gorilla/websocket] ( godoc.org/github.com/gorilla/websocket ) .
128124
129125The 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.
131127
132128### x/net/websocket
133129
@@ -142,12 +138,12 @@ See https://github.com/golang/go/issues/18152
142138https://github.com/gobwas/ws
143139
144140This 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.
146142
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
149145I could into nhooyr/websocket.
150146
151147If you want a library that gives you absolute control over everything, this is the library,
152148but 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