forked from coder/websocket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
198 lines (170 loc) · 4.91 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package websocket_test
import (
"context"
"log"
"net/http"
"time"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
func ExampleAccept() {
// This handler accepts a WebSocket connection, reads a single JSON
// message from the client and then closes the connection.
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
defer cancel()
var v interface{}
err = wsjson.Read(ctx, c, &v)
if err != nil {
log.Println(err)
return
}
c.Close(websocket.StatusNormalClosure, "")
})
err := http.ListenAndServe("localhost:8080", fn)
log.Fatal(err)
}
func ExampleDial() {
// Dials a server, writes a single JSON message and then
// closes the connection.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
log.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
err = wsjson.Write(ctx, c, "hi")
if err != nil {
log.Fatal(err)
}
c.Close(websocket.StatusNormalClosure, "")
}
func ExampleCloseStatus() {
// Dials a server and then expects to be disconnected with status code
// websocket.StatusNormalClosure.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
c, _, err := websocket.Dial(ctx, "ws://localhost:8080", nil)
if err != nil {
log.Fatal(err)
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
_, _, err = c.Reader(ctx)
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
log.Fatalf("expected to be disconnected with StatusNormalClosure but got: %v", err)
}
}
func Example_writeOnly() {
// This handler demonstrates how to correctly handle a write only WebSocket connection.
// i.e you only expect to write messages and do not expect to read any messages.
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "the sky is falling")
ctx, cancel := context.WithTimeout(r.Context(), time.Minute*10)
defer cancel()
ctx = c.CloseRead(ctx)
t := time.NewTicker(time.Second * 30)
defer t.Stop()
for {
select {
case <-ctx.Done():
c.Close(websocket.StatusNormalClosure, "")
return
case <-t.C:
err = wsjson.Write(ctx, c, "hi")
if err != nil {
log.Println(err)
return
}
}
}
})
err := http.ListenAndServe("localhost:8080", fn)
log.Fatal(err)
}
func Example_crossOrigin() {
// This handler demonstrates how to safely accept cross origin WebSockets
// from the origin example.com.
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
OriginPatterns: []string{"example.com"},
})
if err != nil {
log.Println(err)
return
}
c.Close(websocket.StatusNormalClosure, "cross origin WebSocket accepted")
})
err := http.ListenAndServe("localhost:8080", fn)
log.Fatal(err)
}
// This example demonstrates how to create a WebSocket server
// that gracefully exits when sent a signal.
//
// It starts a WebSocket server that keeps every connection open
// for 10 seconds.
// If you CTRL+C while a connection is open, it will wait at most 30s
// for all connections to terminate before shutting down.
// func ExampleGrace() {
// fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// c, err := websocket.Accept(w, r, nil)
// if err != nil {
// log.Println(err)
// return
// }
// defer c.Close(websocket.StatusInternalError, "the sky is falling")
//
// ctx := c.CloseRead(r.Context())
// select {
// case <-ctx.Done():
// case <-time.After(time.Second * 10):
// }
//
// c.Close(websocket.StatusNormalClosure, "")
// })
//
// var g websocket.Grace
// s := &http.Server{
// Handler: g.Handler(fn),
// ReadTimeout: time.Second * 15,
// WriteTimeout: time.Second * 15,
// }
//
// errc := make(chan error, 1)
// go func() {
// errc <- s.ListenAndServe()
// }()
//
// sigs := make(chan os.Signal, 1)
// signal.Notify(sigs, os.Interrupt)
// select {
// case err := <-errc:
// log.Printf("failed to listen and serve: %v", err)
// case sig := <-sigs:
// log.Printf("terminating: %v", sig)
// }
//
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
// defer cancel()
// s.Shutdown(ctx)
// g.Shutdown(ctx)
// }
// This example demonstrates full stack chat with an automated test.
func Example_fullStackChat() {
// https://github.com/nhooyr/websocket/tree/master/examples/chat
}
// This example demonstrates a echo server.
func Example_echo() {
// https://github.com/nhooyr/websocket/tree/master/examples/echo
}