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
99This 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
3433func 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
6872func 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
111118This package is the community standard but it is very old and over timennn
112119has accumulated cruft. There are many ways to do the same thing and the API
113120overall 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
118125and 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
135142This library is fantastic in terms of performance though. The author put in significant
136143effort 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
139146If 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
141148be just as performant but much easier to use.
0 commit comments