mirror of
https://github.com/Luzifer/twitch-manager.git
synced 2024-12-21 04:11:17 +00:00
Allow for different message types
This commit is contained in:
parent
b15cf1bd72
commit
0c9a482716
5 changed files with 26 additions and 7 deletions
16
api.go
16
api.go
|
@ -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
8
app.js
|
@ -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
|
||||||
|
|
3
main.go
3
main.go
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
stats.go
2
stats.go
|
@ -77,7 +77,7 @@ func updateFollowers() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.Wrap(
|
return errors.Wrap(
|
||||||
sendAllSockets(store),
|
sendAllSockets(msgTypeStore, store),
|
||||||
"update all sockets",
|
"update all sockets",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue