Allow for different message types

This commit is contained in:
Knut Ahlers 2020-11-21 00:01:55 +01:00
parent b15cf1bd72
commit 0c9a482716
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
5 changed files with 26 additions and 7 deletions

16
api.go
View file

@ -13,17 +13,24 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
const (
msgTypeStore string = "store"
)
var ( var (
socketSubscriptions = map[string]func(msg interface{}) error{} socketSubscriptions = map[string]func(msg interface{}) error{}
socketSubscriptionsLock = new(sync.RWMutex) socketSubscriptionsLock = new(sync.RWMutex)
) )
func sendAllSockets(msg interface{}) error { func sendAllSockets(msgType string, msg interface{}) error {
socketSubscriptionsLock.RLock() socketSubscriptionsLock.RLock()
defer socketSubscriptionsLock.RUnlock() defer socketSubscriptionsLock.RUnlock()
for _, hdl := range socketSubscriptions { 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") 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") log.WithError(err).Error("Unable to send initial state")
return return
} }

8
app.js
View file

@ -67,7 +67,15 @@ const app = new Vue({
} }
this.socket.onmessage = evt => { this.socket.onmessage = evt => {
const data = JSON.parse(evt.data) const data = JSON.parse(evt.data)
switch (data.type) {
case 'store':
this.store = data this.store = data
break
default:
console.log(`Unhandled message type ${data.type}`, data)
}
} }
this.socket.onopen = evt => { this.socket.onopen = evt => {
this.conn.avail = true this.conn.avail = true

View file

@ -63,6 +63,7 @@ func main() {
registerAPI(router) registerAPI(router)
router.HandleFunc("/{file:(?:app.js|overlay.html)}", func(w http.ResponseWriter, r *http.Request) { 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"])) http.ServeFile(w, r, path.Join(cfg.AssetDir, mux.Vars(r)["file"]))
}) })
@ -85,7 +86,7 @@ func main() {
for { for {
select { select {
case <-timerForceSync.C: 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") log.WithError(err).Error("Unable to send store to all sockets")
} }

View file

@ -77,7 +77,7 @@ func updateFollowers() error {
} }
return errors.Wrap( return errors.Wrap(
sendAllSockets(store), sendAllSockets(msgTypeStore, store),
"update all sockets", "update all sockets",
) )
} }

View file

@ -98,7 +98,7 @@ func handleWebHookPush(w http.ResponseWriter, r *http.Request) {
logger.WithError(err).Error("Unable to update persistent store") 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") logger.WithError(err).Error("Unable to send update to all sockets")
} }
} }