1
- # ws
1
+ # websocket
2
2
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 )
6
6
7
- ws is a minimal and idiomatic WebSocket library for Go.
7
+ websocket is a minimal and idiomatic WebSocket library for Go.
8
8
9
9
This library is in heavy development.
10
10
11
11
## Install
12
12
13
13
``` bash
14
- go get nhooyr.io/ws @master
14
+ go get nhooyr.io/websocket @master
15
15
```
16
16
17
17
## Features
@@ -20,10 +20,9 @@ go get nhooyr.io/ws@master
20
20
- Simple to use because of the minimal API
21
21
- Uses the context package for cancellation
22
22
- 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
25
24
- Highly optimized
26
- - API will be ready for WebSockets over HTTP/2
25
+ - API will transparently work with WebSockets over HTTP/2
27
26
- WASM support
28
27
29
28
## Example
@@ -33,57 +32,65 @@ go get nhooyr.io/ws@master
33
32
``` go
34
33
func main () {
35
34
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
+ )
37
38
if err != nil {
38
39
log.Printf (" server handshake failed: %v " , err)
39
40
return
40
41
}
41
- defer c.Close (ws .StatusInternalError , " " )
42
+ defer c.Close (websocket .StatusInternalError , " " )
42
43
43
44
ctx , cancel := context.WithTimeout (r.Context (), time.Second *10 )
44
45
defer cancel ()
45
46
46
- err = wsjson. Write (ctx, c, map [string ]interface {}{
47
+ v := map [string ]interface {}{
47
48
" my_field" : " foo" ,
48
- })
49
+ }
50
+ err = websocket.WriteJSON (ctx, c, v)
49
51
if err != nil {
50
- log.Printf (" failed to write json struct : %v " , err)
52
+ log.Printf (" failed to write json: %v " , err)
51
53
return
52
54
}
53
55
54
- c.Close (ws.StatusNormalClosure , " " )
56
+ log.Printf (" wrote %v " , v)
57
+
58
+ c.Close (websocket.StatusNormalClosure , " " )
55
59
})
56
- // For production deployments, use a net/http.Server configured
57
- // with the appropriate timeouts.
58
60
err := http.ListenAndServe (" localhost:8080" , fn)
59
61
if err != nil {
60
62
log.Fatalf (" failed to listen and serve: %v " , err)
61
63
}
62
64
}
63
65
```
64
66
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
+
65
69
### Client
66
70
67
71
``` go
68
72
func main () {
69
73
ctx := context.Background ()
70
- ctx , cancel := context.WithTimeout (ctx, time.Minute )
74
+ ctx , cancel := context.WithTimeout (ctx, time.Second * 10 )
71
75
defer cancel ()
72
76
73
- c , _ , err := ws.Dial (ctx, " ws://localhost:8080" )
77
+ c , _ , err := websocket.Dial (ctx, " ws://localhost:8080" ,
78
+ websocket.DialSubprotocols (" test" ),
79
+ )
74
80
if err != nil {
75
81
log.Fatalf (" failed to ws dial: %v " , err)
76
82
}
77
- defer c.Close (ws .StatusInternalError , " " )
83
+ defer c.Close (websocket .StatusInternalError , " " )
78
84
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)
82
87
if err != nil {
83
- log.Fatalf (" failed to write json struct : %v " , err)
88
+ log.Fatalf (" failed to read json: %v " , err)
84
89
}
85
90
86
- c.Close (ws.StatusNormalClosure , " " )
91
+ log.Printf (" received %v " , v)
92
+
93
+ c.Close (websocket.StatusNormalClosure , " " )
87
94
}
88
95
```
89
96
@@ -111,10 +118,10 @@ https://github.com/gorilla/websocket
111
118
This package is the community standard but it is very old and over timennn
112
119
has accumulated cruft. There are many ways to do the same thing and the API
113
120
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
115
122
[ gorilla/websocket] ( godoc.org/github.com/gorilla/websocket ) .
116
123
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
118
125
and with HTTP/2 in mind which makes using it correctly and safely much easier.
119
126
120
127
### x/net/websocket
@@ -134,8 +141,8 @@ and clarity. Its just not clear how to do things in a safe manner.
134
141
135
142
This library is fantastic in terms of performance though. The author put in significant
136
143
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 .
138
145
139
146
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
141
148
be just as performant but much easier to use.
0 commit comments