-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsse.go
43 lines (38 loc) · 850 Bytes
/
sse.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
package sse
import (
"bytes"
"strconv"
)
// Minimal logger that allows you to pass in a *slog.Logger
type logger interface {
Debug(string, ...any)
}
// https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
type Event struct {
ID string // id (optional)
Type string // event type (optional)
Data []byte // data
Retry int // retry (optional)
}
func (e *Event) Format() *bytes.Buffer {
b := new(bytes.Buffer)
if e.ID != "" {
b.WriteString("id: " + e.ID + "\n")
}
if e.Type != "" {
b.WriteString("event: " + e.Type + "\n")
}
if len(e.Data) > 0 {
b.WriteString("data: ")
b.Write(e.Data)
b.WriteByte('\n')
}
if e.Retry > 0 {
b.WriteString("retry: " + strconv.Itoa(e.Retry) + "\n")
}
b.WriteByte('\n')
return b
}
func (e *Event) String() string {
return e.Format().String()
}