can play game

This commit is contained in:
2026-03-02 13:39:18 -07:00
parent 9955a7f90c
commit 0dae393d0d
27 changed files with 856 additions and 474 deletions

View File

@@ -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