-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
132 lines (111 loc) · 2.49 KB
/
stream.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
package sse
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
"strconv"
"time"
)
type Stream struct {
bw *bufio.ReadWriter
w io.Writer
nc net.Conn
}
type BinaryMarshaler interface {
MarshalBinary() ([]byte, error)
}
type TextMarshaler interface {
MarshalText() ([]byte, error)
}
func (s *Stream) Flush() error {
return s.bw.Flush()
}
// Close sends close event with empty data and closes underlying connection.
func (s *Stream) Close() error {
defer s.nc.Close()
_, err := s.bw.Write([]byte("event:close\ndata:\n\n"))
if err != nil {
return err
}
return s.Flush()
}
func (s *Stream) SetRetry(retry time.Duration) error {
data := fmt.Sprintf("retry:%v\n", retry.Milliseconds())
_, err := s.bw.Write([]byte(data))
return err
}
func (s *Stream) SetID(id int64) error {
str := strconv.FormatInt(id, 10)
data := []byte("id:" + str + "\n")
_, err := s.bw.Write(data)
return err
}
func (s *Stream) SetEvent(event string) error {
data := []byte("event:" + event + "\n")
_, err := s.bw.Write(data)
return err
}
func (s *Stream) WriteEvent(id int64, event string, data []byte) error {
if err := s.SetID(id); err != nil {
return err
}
if err := s.SetEvent(event); err != nil {
return err
}
if err := s.WriteBytes(data); err != nil {
return err
}
return s.Flush()
}
func (s *Stream) WriteJSON(v interface{}) error {
raw, err := json.Marshal(v)
if err != nil {
return err
}
return s.writeData(raw)
}
func (s *Stream) WriteBinary(message BinaryMarshaler) error {
data, err := message.MarshalBinary()
if err != nil {
return err
}
return s.writeData(data)
}
func (s *Stream) WriteText(message TextMarshaler) error {
text, err := message.MarshalText()
if err != nil {
return err
}
return s.writeData(text)
}
func (s *Stream) WriteBytes(data []byte) error {
return s.writeData(data)
}
func (s *Stream) WriteString(data string) error {
return s.writeData([]byte(data))
}
func (s *Stream) WriteInt(num int64) error {
str := strconv.FormatInt(num, 10)
return s.writeData([]byte(str))
}
func (s *Stream) WriteFloat(num float64) error {
str := strconv.FormatFloat(num, 'f', 5, 64)
return s.writeData([]byte(str))
}
func (s *Stream) writeData(data []byte) error {
size := 6 + 1 + len(data) // size of "data\n\n" + ":{data}"
buf := make([]byte, 0, size)
buf = append(buf, "data"...)
if len(data) > 0 {
buf = append(buf, ':')
buf = append(buf, data...)
}
buf = append(buf, []byte("\n\n")...)
_, err := s.bw.Write(buf)
if err != nil {
return err
}
return s.bw.Flush()
}