summaryrefslogtreecommitdiff
path: root/lib/lsg_web
diff options
context:
space:
mode:
authorhref <href@random.sh>2018-02-02 21:35:46 +0100
committerhref <href@random.sh>2018-02-02 21:35:46 +0100
commitc98ddad3263f6859af3c5c76fa197876743d5ded (patch)
tree9e20617da1089e918e0445162dc9bd8eb54f3459 /lib/lsg_web
parentimport (diff)
sse / embedded player
Diffstat (limited to '')
-rw-r--r--lib/lsg_web/controllers/icecast_see_controller.ex41
-rw-r--r--lib/lsg_web/endpoint.ex2
-rw-r--r--lib/lsg_web/router.ex11
-rw-r--r--lib/lsg_web/templates/page/widget.html.eex11
4 files changed, 58 insertions, 7 deletions
diff --git a/lib/lsg_web/controllers/icecast_see_controller.ex b/lib/lsg_web/controllers/icecast_see_controller.ex
new file mode 100644
index 0000000..1eecca1
--- /dev/null
+++ b/lib/lsg_web/controllers/icecast_see_controller.ex
@@ -0,0 +1,41 @@
+defmodule LSGWeb.IcecastSseController do
+ use LSGWeb, :controller
+ require Logger
+
+ @ping_interval 20_000
+
+ def sse(conn, _params) do
+ conn
+ |> put_resp_header("X-Accel-Buffering", "no")
+ |> put_resp_header("content-type", "text/event-stream")
+ |> send_chunked(200)
+ |> subscribe
+ |> send_sse_message("ping", "ping")
+ |> send_sse_message("icecast", LSG.IcecastAgent.get)
+ |> sse_loop
+ end
+
+ def subscribe(conn) do
+ :timer.send_interval(@ping_interval, {:event, :ping})
+ {:ok, _} = Registry.register(LSG.BroadcastRegistry, "icecast", [])
+ conn
+ end
+
+ def sse_loop(conn) do
+ {type, event} = receive do
+ {:event, :ping} -> {"ping", "ping"}
+ {:icecast, stats} -> {"icecast", stats}
+ end
+
+ conn
+ |> send_sse_message(type, event)
+ |> sse_loop()
+ end
+
+ defp send_sse_message(conn, type, data) do
+ json = Jason.encode!(%{type => data})
+ {:ok, conn} = chunk(conn, "event: #{type}\ndata: #{json}\n\n")
+ conn
+ end
+
+end
diff --git a/lib/lsg_web/endpoint.ex b/lib/lsg_web/endpoint.ex
index e05e3f5..2d0a6be 100644
--- a/lib/lsg_web/endpoint.ex
+++ b/lib/lsg_web/endpoint.ex
@@ -13,7 +13,7 @@ defmodule LSGWeb.Endpoint do
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
- if code_reloading? do
+ if 42==43 && code_reloading? do
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
diff --git a/lib/lsg_web/router.ex b/lib/lsg_web/router.ex
index 773a4c2..db0e5cd 100644
--- a/lib/lsg_web/router.ex
+++ b/lib/lsg_web/router.ex
@@ -3,14 +3,14 @@ defmodule LSGWeb.Router do
pipeline :browser do
plug :accepts, ["html"]
- plug :fetch_session
- plug :fetch_flash
- plug :protect_from_forgery
- plug :put_secure_browser_headers
+ #plug :fetch_session
+ #plug :fetch_flash
+ #plug :protect_from_forgery
+ #plug :put_secure_browser_headers
end
pipeline :api do
- plug :accepts, ["json"]
+ plug :accepts, ["json", "sse"]
end
scope "/", LSGWeb do
@@ -22,5 +22,6 @@ defmodule LSGWeb.Router do
scope "/api", LSGWeb do
pipe_through :api
get "/icecast.json", PageController, :icecast
+ get "/icecast.sse", IcecastSseController, :sse
end
end
diff --git a/lib/lsg_web/templates/page/widget.html.eex b/lib/lsg_web/templates/page/widget.html.eex
index efa382f..65853b3 100644
--- a/lib/lsg_web/templates/page/widget.html.eex
+++ b/lib/lsg_web/templates/page/widget.html.eex
@@ -5,7 +5,16 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<%= static_path(@conn, "/assets/css/widget.css") %>">
+<script src="<%= static_path(@conn, "/assets/js/soundmanager2-jsmin.js") %>"></script>
<script src="<%= static_path(@conn, "/assets/js/widget.js") %>"></script>
</head>
-<body></body>
+<body>
+ <div id="player" class="<%= if @icecast.live, do: "live", else: "autodj" %>">
+ <div id="state">▶</div>
+ <div id="titles">
+ <div id="genre"><%= @icecast.genre %></div>
+ <div id="np"><%= @icecast.np %></div>
+ </div>
+ </div>
+</body>
</html>