forked from alexandrevicenzi/go-sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_http_options.go
54 lines (46 loc) · 1.17 KB
/
net_http_options.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
package main
import (
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/alexandrevicenzi/go-sse"
)
func main() {
s := sse.NewServer(&sse.Options{
// Increase default retry interval to 10s.
RetryInterval: 10 * 1000,
// CORS headers
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Keep-Alive,X-Requested-With,Cache-Control,Content-Type,Last-Event-ID",
},
// Custom channel name generator
ChannelNameFunc: func(request *http.Request) string {
return request.URL.Path
},
// Print debug info
Logger: log.New(os.Stdout, "go-sse: ", log.Ldate|log.Ltime|log.Lshortfile),
})
defer s.Shutdown()
http.Handle("/", http.FileServer(http.Dir("./static")))
http.Handle("/events/", s)
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)
}
}()
log.Println("Listening at :3000")
http.ListenAndServe(":3000", nil)
}