From 0c9a482716793ce562ab36e49c77f84f4558489c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 21 Nov 2020 00:01:55 +0100 Subject: [PATCH] Allow for different message types --- api.go | 16 +++++++++++++--- app.js | 10 +++++++++- main.go | 3 ++- stats.go | 2 +- webhook.go | 2 +- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/api.go b/api.go index 533aa47..beb932f 100644 --- a/api.go +++ b/api.go @@ -13,17 +13,24 @@ import ( "github.com/gorilla/websocket" ) +const ( + msgTypeStore string = "store" +) + var ( socketSubscriptions = map[string]func(msg interface{}) error{} socketSubscriptionsLock = new(sync.RWMutex) ) -func sendAllSockets(msg interface{}) error { +func sendAllSockets(msgType string, msg interface{}) error { socketSubscriptionsLock.RLock() defer socketSubscriptionsLock.RUnlock() for _, hdl := range socketSubscriptions { - if err := hdl(msg); err != nil { + if err := hdl(map[string]interface{}{ + "payload": msg, + "type": msgType, + }); err != nil { return errors.Wrap(err, "submit message") } } @@ -80,7 +87,10 @@ func handleUpdateSocket(w http.ResponseWriter, r *http.Request) { } }() - if err := conn.WriteJSON(store); err != nil { + if err := conn.WriteJSON(map[string]interface{}{ + "payload": store, + "type": msgTypeStore, + }); err != nil { log.WithError(err).Error("Unable to send initial state") return } diff --git a/app.js b/app.js index 4a8d0c5..f8f96fe 100644 --- a/app.js +++ b/app.js @@ -67,7 +67,15 @@ const app = new Vue({ } this.socket.onmessage = evt => { const data = JSON.parse(evt.data) - this.store = data + + switch (data.type) { + case 'store': + this.store = data + break + + default: + console.log(`Unhandled message type ${data.type}`, data) + } } this.socket.onopen = evt => { this.conn.avail = true diff --git a/main.go b/main.go index 9812f6b..cfaa918 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,7 @@ func main() { registerAPI(router) router.HandleFunc("/{file:(?:app.js|overlay.html)}", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-cache") http.ServeFile(w, r, path.Join(cfg.AssetDir, mux.Vars(r)["file"])) }) @@ -85,7 +86,7 @@ func main() { for { select { case <-timerForceSync.C: - if err := sendAllSockets(store); err != nil { + if err := sendAllSockets(msgTypeStore, store); err != nil { log.WithError(err).Error("Unable to send store to all sockets") } diff --git a/stats.go b/stats.go index 599548b..8c50d96 100644 --- a/stats.go +++ b/stats.go @@ -77,7 +77,7 @@ func updateFollowers() error { } return errors.Wrap( - sendAllSockets(store), + sendAllSockets(msgTypeStore, store), "update all sockets", ) } diff --git a/webhook.go b/webhook.go index 0dfd916..4539676 100644 --- a/webhook.go +++ b/webhook.go @@ -98,7 +98,7 @@ func handleWebHookPush(w http.ResponseWriter, r *http.Request) { logger.WithError(err).Error("Unable to update persistent store") } - if err := sendAllSockets(store); err != nil { + if err := sendAllSockets(msgTypeStore, store); err != nil { logger.WithError(err).Error("Unable to send update to all sockets") } }