-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (97 loc) · 2.62 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"
"github.com/programming-in-th/rtss/ws"
)
// Client is a middleman between the websocket connection and the hub.
type Client struct {
id uint64
hub *Hub
send chan Payload
}
type Payload struct {
Id uint64 `json:"id"`
Groups []Group `json:"groups"`
Score int64 `json:"score"`
Status string `json:"status"`
}
func SSE(hub *Hub, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
rawId := r.URL.Query().Get("id")
id, _ := strconv.ParseUint(rawId, 10, 32)
client := &Client{id: id, hub: hub, send: make(chan Payload, 256)}
client.hub.register <- client
defer func() {
client.hub.unregister <- client
}()
timeout := time.After(600 * time.Second)
for {
select {
case message := <-client.send:
fmt.Fprintf(w, "data: %s\n\n", PayloadToJSONString(message))
w.(http.Flusher).Flush()
case <-r.Context().Done():
return
case <-timeout:
return
}
}
}
func main() {
hub := newHub()
go hub.run()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("defaulting to port %s", port)
}
wsHost := os.Getenv("WS_HOST")
if wsHost == "" {
wsHost = "157.230.244.51:4000"
}
wsPath := os.Getenv("WS_PATH")
if wsPath == "" {
wsPath = "/socket/websocket"
}
go func() {
u := url.URL{Scheme: "ws", Host: wsHost, Path: wsPath}
s := &ws.Socket{UrlString: u}
s.Connect()
defer s.Connection.Close()
channel := s.SetChannel("realtime:public:Submission")
channel.Join()
channel.On("*", func(data interface{}) {
d := data.(map[string]interface{})["record"]
if d != nil {
rawId := d.(map[string]interface{})["id"].(string)
id, _ := strconv.ParseUint(rawId, 10, 32)
raw := d.(map[string]interface{})["groups"]
if raw.(string) == "unchanged_toast" {
o := data.(map[string]interface{})["old_record"]
raw = o.(map[string]interface{})["groups"]
}
var groups []Group
json.Unmarshal([]byte(raw.(string)), &groups)
score, _ := strconv.ParseInt(d.(map[string]interface{})["score"].(string), 10, 32)
payload := Payload{Id: id, Groups: groups, Score: score, Status: d.(map[string]interface{})["status"].(string)}
hub.broadcast <- payload
}
})
s.Listen()
}()
http.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) {
SSE(hub, w, r)
})
log.Fatal("HTTP server error: ", http.ListenAndServe(":"+port, nil))
}