From 64f477f42385e05c21f01825f23fba0fa81d5cb9 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 5 Sep 2019 22:08:10 -0700 Subject: [PATCH] Send server stat updates over the websocket --- server/environment_docker.go | 3 +++ server/events.go | 1 + websocket.go | 10 ++++++++++ 3 files changed, 14 insertions(+) diff --git a/server/environment_docker.go b/server/environment_docker.go index a558c40..d0a69c1 100644 --- a/server/environment_docker.go +++ b/server/environment_docker.go @@ -317,6 +317,9 @@ func (d *DockerEnvironment) EnableResourcePolling() error { s.Resources.Network.RxBytes += nw.RxBytes s.Resources.Network.TxBytes += nw.TxBytes } + + b, _ := json.Marshal(s.Resources) + s.Emit(StatsEvent, string(b)) } }(d.Server) diff --git a/server/events.go b/server/events.go index ce8485a..948e6e5 100644 --- a/server/events.go +++ b/server/events.go @@ -9,6 +9,7 @@ type EventListenerFunction *func(string) const ( ConsoleOutputEvent = "console output" StatusEvent = "status" + StatsEvent = "stats" ) // Adds an event listener for the server instance. diff --git a/websocket.go b/websocket.go index 4586755..c411b9a 100644 --- a/websocket.go +++ b/websocket.go @@ -71,12 +71,22 @@ func (rt *Router) routeWebsocket(w http.ResponseWriter, r *http.Request, ps http }) } + handleResourceUse := func(data string) { + handler.SendJson(&WebsocketMessage{ + Event: server.StatsEvent, + Args: []string{data}, + }) + } + s.AddListener(server.StatusEvent, &handleServerStatus) defer s.RemoveListener(server.StatusEvent, &handleServerStatus) s.AddListener(server.ConsoleOutputEvent, &handleOutput) defer s.RemoveListener(server.ConsoleOutputEvent, &handleOutput) + s.AddListener(server.StatsEvent, &handleResourceUse) + defer s.RemoveListener(server.StatsEvent, &handleResourceUse) + s.Emit(server.StatusEvent, s.State) for {