Call broadcast function from app configured module.

This commit is contained in:
Pim Kunis 2019-08-24 16:01:49 +02:00
parent d6e877137f
commit fc54fa1761
7 changed files with 15 additions and 22 deletions

View file

@ -28,6 +28,8 @@ config :logger, backends: [RingLogger]
config :phoenix, :json_library, Jason config :phoenix, :json_library, Jason
config :morse, :progress_socket, UiWeb.UserSocket
if Mix.target() != :host do if Mix.target() != :host do
"target.exs" "target.exs"
else else

View file

@ -30,7 +30,11 @@ defmodule Morse.Server do
@impl true @impl true
def handle_cast({:progress, new_progress}, {pid, _progress}) do def handle_cast({:progress, new_progress}, {pid, _progress}) do
GenServer.cast(Ui.SocketAPI, {:broadcast_progress, new_progress}) apply(progress_socket(), :broadcast_progress, [new_progress])
{:noreply, {pid, new_progress}} {:noreply, {pid, new_progress}}
end end
defp progress_socket do
Application.fetch_env!(:morse, :progress_socket)
end
end end

View file

@ -49,7 +49,7 @@ defmodule Morse.Worker do
Process.sleep(@sleep_delay) Process.sleep(@sleep_delay)
end end
defp signal_symbol(_gpio, {? , index}, length) do defp signal_symbol(_gpio, {?\s, index}, length) do
Process.sleep(@sleep_pause) Process.sleep(@sleep_pause)
update_progress(index, length) update_progress(index, length)
end end

View file

@ -9,8 +9,7 @@ defmodule Ui.Application do
# List all child processes to be supervised # List all child processes to be supervised
children = [ children = [
# Start the endpoint when the application starts # Start the endpoint when the application starts
UiWeb.Endpoint, UiWeb.Endpoint
Ui.SocketAPI
# Starts a worker by calling: Ui.Worker.start_link(arg) # Starts a worker by calling: Ui.Worker.start_link(arg)
# {Ui.Worker, arg}, # {Ui.Worker, arg},
] ]

View file

@ -1,18 +0,0 @@
defmodule Ui.SocketAPI do
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
@impl true
def init(state) do
{:ok, state}
end
@impl true
def handle_cast({:broadcast_progress, progress}, state) do
UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress})
{:noreply, state}
end
end

View file

@ -30,4 +30,9 @@ defmodule UiWeb.UserSocket do
# #
# Returning `nil` makes this socket anonymous. # Returning `nil` makes this socket anonymous.
def id(_socket), do: nil def id(_socket), do: nil
def broadcast_progress(progress) do
IO.puts("in user socket!")
UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress})
end
end end

View file

@ -17,6 +17,7 @@ defmodule UiWeb.Endpoint do
# Code reloading can be explicitly enabled under the # Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint. # :code_reloader configuration of your endpoint.
IO.inspect code_reloading?
if code_reloading? do if code_reloading? do
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader plug Phoenix.LiveReloader