-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsse.go
84 lines (66 loc) · 1.5 KB
/
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
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
package sse
import (
"bufio"
"time"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp"
)
type Options struct {
// Tick of each event
Tick func(c *fiber.Ctx, w *bufio.Writer, close func())
// Closed by unknown reason. e.g. client is closed
OnClosed func(c *fiber.Ctx, err error)
// Term of each tick. Default is 1 second
Sleep time.Duration
}
/*
Example:
i := 0
app.Get("/sse", func(c *fiber.Ctx) error {
i := 0
return sse.Handler(c, sse.Options{
Tick: func(c *fiber.Ctx, w *bufio.Writer, close func()) {
i++
if i > 3 {
close()
return
}
fmt.Fprintf(w, "data: %d - %s\n\n", i, time.Now().Format(time.RFC3339Nano))
},
OnClosed: func(c *fiber.Ctx, err error) {
fmt.Println("Connection is closed:", err)
},
Sleep: time.Second,
})
})
*/
func Handler(c *fiber.Ctx, options Options) error {
if options.Sleep == 0 {
options.Sleep = time.Second
}
if options.OnClosed == nil {
options.OnClosed = func(c *fiber.Ctx, err error) {}
}
closeFlag := false
close := func() {
closeFlag = true
}
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")
c.Context().SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {
for {
if closeFlag {
return
}
options.Tick(c, w, close)
if err := w.Flush(); err != nil {
options.OnClosed(c, err)
break
}
time.Sleep(options.Sleep)
}
}))
return nil
}