Garuda v0.2.5 Garuda.GameChannel behaviour

Defines specific game behaviours over Phoenix.Channel.

GameChannel extends Phoenix.Channel and defines game-behaviours.

Using GameChannel

defmodule TictactoePhxWeb.TictactoeChannel do
  use Garuda.GameChannel

  @impl true
  def on_join(_params, _socket) do
    IO.puts("Player Joined")
  end

  @impl true
  def on_rejoin(_params, _socket) do
    IO.puts("Player rejoined")
  end

  @impl true
  def authorized?(_params) do
    # Custom authorization code
    true
  end

  @impl true
  def handle_in("player_move", cell, socket) do
    # Handling usual events from client
    {:noreply, socket}
  end
end

You might have noticed that instead of use Phoenix.Channel, we are using Garuda.GameChannel.

Link to this section Summary

Functions

Returns the process id of game-room

Callbacks

Verifies the channel connection

handles game-channel join.

handles game-channel re-join.

Link to this section Functions

Returns the process id of game-room

  • socket - socket state of game-channel

Link to this section Callbacks

Link to this callback

authorized?(params)

Specs

authorized?(params :: map()) :: boolean()

Verifies the channel connection

Channel connection is only established , if authorized?, returns true. params is the object that is send from the client.

Link to this callback

on_join(params, socket)

Specs

on_join(params :: map(), socket :: Phoenix.Socket) :: any()

handles game-channel join.

on_join is called after socket connection is established successfully.

Link to this callback

on_rejoin(params, socket)

Specs

on_rejoin(params :: map(), socket :: Phoenix.Socket) :: any()

handles game-channel re-join.

Called when a player re-joins the game-channel, ex after network reconnection.