feature: Refresh dashboards when entity files change. (#319)

* feature: Refresh dashboards when entities change

* feature: Refresh dashboards when entities change

* bugfix: Concurrency, lock around websocket write
This commit is contained in:
James Read
2024-05-24 23:10:38 +01:00
committed by GitHub
parent 18423a9888
commit 3904f8563d
7 changed files with 74 additions and 12 deletions

View File

@@ -8,12 +8,17 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
"net/http"
"sync"
)
var upgrader = ws.Upgrader{
CheckOrigin: checkOriginPermissive,
}
var (
sendmutex = sync.Mutex{}
)
type WebsocketClient struct {
conn *ws.Conn
}
@@ -44,6 +49,10 @@ func (WebsocketExecutionListener) OnExecutionStarted(title string) {
*/
}
func OnEntityChanged() {
broadcast(&pb.EventEntityChanged{})
}
/*
The default checkOrigin function checks that the origin (browser) matches the
request origin. However in OliveTin we expect many users to deliberately proxy
@@ -113,9 +122,11 @@ func broadcast(pbmsg protoreflect.ProtoMessage) {
hackyMessage = append(hackyMessage, []byte("}")...)
// </EVIL>
sendmutex.Lock()
for _, client := range clients {
client.conn.WriteMessage(ws.TextMessage, hackyMessage)
}
sendmutex.Unlock()
}
func (c *WebsocketClient) messageLoop() {