can play game
This commit is contained in:
@@ -1,36 +1,27 @@
|
||||
defmodule BackendWeb.UserSocket do
|
||||
use Phoenix.Socket
|
||||
require Logger
|
||||
|
||||
# A Socket handler
|
||||
#
|
||||
# It's possible to control the websocket connection and
|
||||
# assign values that can be accessed by your channel topics.
|
||||
|
||||
## Channels
|
||||
channel("game:*", BackendWeb.GameChannel)
|
||||
channel("user:*", BackendWeb.ConnectedUserChannel)
|
||||
|
||||
# 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` or `{:error, term}`.
|
||||
@impl true
|
||||
def connect(_params, socket, _connect_info) do
|
||||
{:ok, socket}
|
||||
def connect(%{"user_name" => user_name}, socket, _connect_info) do
|
||||
{:ok, assign(socket, :user_name, user_name)}
|
||||
end
|
||||
|
||||
def connect(_params, _socket, _connect_info) do
|
||||
Logger.warning("WebSocket connection rejected: user_name required")
|
||||
{:error, %{reason: "user_name required"}}
|
||||
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:
|
||||
#
|
||||
#
|
||||
# Returning `nil` makes this socket anonymous.
|
||||
@impl true
|
||||
def id(%{assigns: %{user_name: user_name}}) do
|
||||
# assign websocket to user name
|
||||
# allows other parts of app to manipulate all sockets for a given user
|
||||
"user_socket:#{user_name}"
|
||||
end
|
||||
|
||||
def id(_socket), do: nil
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user