-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
106 lines (91 loc) · 2.43 KB
/
client.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
/*
Package client implements a server-sent event API client.
*/
package client // import "a4.io/ssse/pkg/client"
import (
"bufio"
"bytes"
"errors"
"net/http"
"net/url"
)
// ErrInvalidSubscribeArgs is returned when a client tries to subscribe without a channel or a callback
var ErrInvalidSubscribeArgs = errors.New("must provides at leat a channel or a callback func in order to subscribe")
var (
headerEvent = []byte("event:")
headerData = []byte("data:")
)
// SSEClient holds the client state
type SSEClient struct {
url string
Username, Password string
}
// Event holds the event fields
type Event struct {
Event string // Type of the event (i.e. "hearbeat", or you custom event type)
Data []byte // Data field
}
// New initialize a new client
func New(url string) *SSEClient {
return &SSEClient{url: url}
}
// Subscribe connects to the server-sent event endpoint.
func (c *SSEClient) Subscribe(events chan<- *Event, callback func(*Event) error, filterEvents ...string) error {
if events == nil && callback == nil {
return ErrInvalidSubscribeArgs
}
vs := url.Values{}
for _, evt := range filterEvents {
vs.Add("event", evt)
}
req, err := http.NewRequest("GET", c.url+"?"+vs.Encode(), nil)
if err != nil {
return err
}
if c.Username != "" || c.Password != "" {
req.SetBasicAuth(c.Username, c.Password)
}
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
var event *Event
for {
// Read each new line and process the type of event
line, err := reader.ReadBytes('\n')
if err != nil {
return err
}
switch {
case bytes.HasPrefix(line, headerEvent):
if event == nil {
event = &Event{}
}
// Remove header
eventType := bytes.Replace(line, headerEvent, []byte(""), 1)
event.Event = string(eventType[1 : len(eventType)-1]) // Remove initial space and newline
case bytes.HasPrefix(line, headerData):
if event == nil {
event = &Event{}
}
// Remove header
data := bytes.Replace(line, headerData, []byte(""), 1)
event.Data = data[1 : len(data)-1] // Remove initial space and newline
default:
if event != nil && event.Event != "heartbeat" {
if events != nil {
events <- event
} else {
if err := callback(event); err != nil {
return err
}
}
event = nil
}
}
}
}