forked from alexandrevicenzi/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
42 lines (34 loc) · 720 Bytes
/
echo.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
package main
import (
"strconv"
"time"
"github.com/alexandrevicenzi/go-sse"
"github.com/labstack/echo/v4"
)
func main() {
s := sse.NewServer(nil)
defer s.Shutdown()
e := echo.New()
e.File("/", "./static/index.html")
e.Any("/events/:channel", func(c echo.Context) error {
req := c.Request()
res := c.Response()
s.ServeHTTP(res, req)
return nil
})
go func() {
for {
s.SendMessage("/events/channel-1", sse.SimpleMessage(time.Now().Format("2006/02/01/ 15:04:05")))
time.Sleep(5 * time.Second)
}
}()
go func() {
i := 0
for {
i++
s.SendMessage("/events/channel-2", sse.SimpleMessage(strconv.Itoa(i)))
time.Sleep(5 * time.Second)
}
}()
e.Logger.Fatal(e.Start(":3000"))
}