initial commit
This commit is contained in:
parent
d91e8e7dcd
commit
836e828371
53 changed files with 2142 additions and 0 deletions
62
lib/demo_web/channels/room_channel.ex
Normal file
62
lib/demo_web/channels/room_channel.ex
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
defmodule DemoWeb.RoomChannel do
|
||||
use DemoWeb, :channel
|
||||
require Logger
|
||||
|
||||
@impl true
|
||||
def join("room:lobby", payload, socket) do
|
||||
if authorized?(payload) do
|
||||
{:ok, socket}
|
||||
else
|
||||
{:error, %{reason: "unauthorized"}}
|
||||
end
|
||||
end
|
||||
|
||||
# Channels can be used in a request/response fashion
|
||||
# by sending replies to requests from the client
|
||||
@impl true
|
||||
def handle_in("ping", payload, socket) do
|
||||
{:reply, {:ok, payload}, socket}
|
||||
end
|
||||
|
||||
#
|
||||
# Here is recieved the requests from the client
|
||||
# we spawn a new process to compute the result, which
|
||||
# is sent back in a new message when the task is finished
|
||||
# The new process PID is used as a key for the client to
|
||||
# associate the result to its request.
|
||||
# the key(id) is immediatly returned to the client.
|
||||
#
|
||||
@impl true
|
||||
def handle_in("fib", payload, socket) do
|
||||
me = self()
|
||||
id = spawn(fn ->
|
||||
result = Funcs.fib(String.to_integer(payload["query"]))
|
||||
send(me, {:response, %{id: Kernel.inspect(self()),
|
||||
query: payload["query"],
|
||||
result: result}})
|
||||
end)
|
||||
{:reply, {:ok, %{id: Kernel.inspect(id)}}, socket}
|
||||
end
|
||||
|
||||
# It is also common to receive messages from the client and
|
||||
# broadcast to everyone in the current topic (room:lobby).
|
||||
@impl true
|
||||
def handle_in("shout", payload, socket) do
|
||||
broadcast(socket, "shout", payload)
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
# Add authorization logic here as required.
|
||||
defp authorized?(_payload) do
|
||||
true
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:response, map}, socket) do
|
||||
Logger.debug("send response #{Kernel.inspect map}")
|
||||
push(socket, "response", map)
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
41
lib/demo_web/channels/user_socket.ex
Normal file
41
lib/demo_web/channels/user_socket.ex
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
defmodule DemoWeb.UserSocket do
|
||||
use Phoenix.Socket
|
||||
|
||||
# A Socket handler
|
||||
#
|
||||
# It's possible to control the websocket connection and
|
||||
# assign values that can be accessed by your channel topics.
|
||||
|
||||
## Channels
|
||||
|
||||
channel "room:*", DemoWeb.RoomChannel
|
||||
|
||||
# Socket params are passed from the client and can
|
||||
# be used to verify and authenticate a user. After
|
||||
# verification, you can put default assigns into
|
||||
# the socket that will be set for all channels, ie
|
||||
#
|
||||
# {:ok, assign(socket, :user_id, verified_user_id)}
|
||||
#
|
||||
# To deny connection, return `:error`.
|
||||
#
|
||||
# See `Phoenix.Token` documentation for examples in
|
||||
# performing token verification on connect.
|
||||
@impl true
|
||||
def connect(_params, socket, _connect_info) do
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
# Socket id's are topics that allow you to identify all sockets for a given user:
|
||||
#
|
||||
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
|
||||
#
|
||||
# Would allow you to broadcast a "disconnect" event and terminate
|
||||
# all active sockets and channels for a given user:
|
||||
#
|
||||
# Elixir.DemoWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
|
||||
#
|
||||
# Returning `nil` makes this socket anonymous.
|
||||
@impl true
|
||||
def id(_socket), do: nil
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue