93 lines
2.2 KiB
Elixir
93 lines
2.2 KiB
Elixir
|
Application.put_env(:sample, Sfe.Endpoint,
|
||
|
http: [ip: {127, 0, 0, 1}, port: 5001],
|
||
|
adapter: Bandit.PhoenixAdapter,
|
||
|
server: true,
|
||
|
live_view: [signing_salt: "aaaaaaaa"],
|
||
|
secret_key_base: String.duplicate("a", 64)
|
||
|
)
|
||
|
|
||
|
Mix.install([
|
||
|
{:bandit, "~> 1.2"},
|
||
|
{:jason, "~> 1.0"},
|
||
|
{:phoenix, "~> 1.7.0"},
|
||
|
{:phoenix_live_view, "~> 0.19.0"},
|
||
|
{:phoenix_pubsub, "~> 2.1"}
|
||
|
])
|
||
|
|
||
|
defmodule Sfe.ErrorView do
|
||
|
def render(template, _), do: Phoenix.Controller.status_message_from_template(template)
|
||
|
end
|
||
|
|
||
|
defmodule Sfe.HomeLive do
|
||
|
use Phoenix.LiveView, layout: {__MODULE__, :live}
|
||
|
|
||
|
def mount(_params, _session, socket) do
|
||
|
{:ok, assign(socket, :count, 0)}
|
||
|
end
|
||
|
|
||
|
defp phx_vsn, do: Application.spec(:phoenix, :vsn)
|
||
|
defp lv_vsn, do: Application.spec(:phoenix_live_view, :vsn)
|
||
|
|
||
|
def render("live.html", assigns) do
|
||
|
~H"""
|
||
|
<script src={"https://cdn.jsdelivr.net/npm/phoenix@#{phx_vsn()}/priv/static/phoenix.min.js"}></script>
|
||
|
<script src={"https://cdn.jsdelivr.net/npm/phoenix_live_view@#{lv_vsn()}/priv/static/phoenix_live_view.min.js"}></script>
|
||
|
<script>
|
||
|
let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket)
|
||
|
liveSocket.connect()
|
||
|
</script>
|
||
|
<style>
|
||
|
* { font-size: 1.1em; }
|
||
|
</style>
|
||
|
<%= @inner_content %>
|
||
|
"""
|
||
|
end
|
||
|
|
||
|
def render(assigns) do
|
||
|
~H"""
|
||
|
Multiplayer Counter
|
||
|
<%= @count %>
|
||
|
<button phx-click="inc">+</button>
|
||
|
<button phx-click="dec">-</button>
|
||
|
"""
|
||
|
end
|
||
|
|
||
|
def handle_event("inc", _params, socket) do
|
||
|
{:noreply, assign(socket, :count, socket.assigns.count + 1)}
|
||
|
end
|
||
|
|
||
|
def handle_event("dec", _params, socket) do
|
||
|
{:noreply, assign(socket, :count, socket.assigns.count - 1)}
|
||
|
end
|
||
|
end
|
||
|
|
||
|
defmodule Sfe.Router do
|
||
|
use Phoenix.Router
|
||
|
import Phoenix.LiveView.Router
|
||
|
|
||
|
pipeline :browser do
|
||
|
plug(:accepts, ["html"])
|
||
|
end
|
||
|
|
||
|
scope "/", Sfe do
|
||
|
pipe_through(:browser)
|
||
|
|
||
|
live("/", HomeLive, :index)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
defmodule Sfe.Endpoint do
|
||
|
use Phoenix.Endpoint, otp_app: :sample
|
||
|
socket("/live", Phoenix.LiveView.Socket)
|
||
|
plug(Sfe.Router)
|
||
|
end
|
||
|
|
||
|
children = [
|
||
|
Sfe.Endpoint,
|
||
|
{Phoenix.PubSub, name: :my_pubsub}
|
||
|
]
|
||
|
|
||
|
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
|
||
|
|
||
|
Process.sleep(:infinity)
|