forked from r3labs/sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
73 lines (65 loc) · 1.7 KB
/
event.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package sse
import (
"bufio"
"bytes"
"io"
"time"
)
// Event holds all of the event source fields
type Event struct {
ID []byte
Data []byte
Event []byte
Retry []byte
timestamp time.Time
}
// EventStreamReader scans an io.Reader looking for EventStream messages.
type EventStreamReader struct {
scanner *bufio.Scanner
buffer []byte
idx int
}
// NewEventStreamReader creates an instance of EventStreamReader.
func NewEventStreamReader(eventStream io.Reader) *EventStreamReader {
scanner := bufio.NewScanner(eventStream)
split := func(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
// We have a full event payload to parse.
if i := bytes.Index(data, []byte("\r\n\r\n")); i >= 0 {
return i + 1, data[0:i], nil
}
if i := bytes.Index(data, []byte("\r\r")); i >= 0 {
return i + 1, data[0:i], nil
}
if i := bytes.Index(data, []byte("\n\n")); i >= 0 {
return i + 1, data[0:i], nil
}
// If we're at EOF, we have all of the data.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
// Set the split function for the scanning operation.
scanner.Split(split)
return &EventStreamReader{
scanner: scanner,
}
}
// ReadEvent scans the EventStream for events.
func (e *EventStreamReader) ReadEvent() ([]byte, error) {
if e.scanner.Scan() {
event := e.scanner.Bytes()
return event, nil
}
if err := e.scanner.Err(); err != nil {
return nil, err
}
return nil, io.EOF
}