-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
57 lines (48 loc) · 915 Bytes
/
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
package sse_test
import (
"log"
"net"
"net/http"
"github.com/cristalhq/sse"
)
func ExampleUpgradeHTTP() {
http.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
stream, err := sse.UpgradeHTTP(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data := struct {
Text string `json:"text"`
}{
Text: "hey there",
}
stream.WriteJSON(data)
})
}
func ExampleUpgrader() {
ln, err := net.Listen("tcp", "localhost:8080")
if err != nil {
panic(err)
}
var u sse.Upgrader
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("accept err: %#v", err)
continue
}
stream, err := u.Upgrade(conn)
if err != nil {
log.Printf("upgrade err: %#v", err)
continue
}
go func() {
defer stream.Close()
err := stream.WriteString(`hey there`)
if err != nil {
log.Printf("send err: %#v", err)
}
}()
}
}