当乐游戏中心

当前位置:龙8官网long8 > 当乐游戏中心 > 简单Elixir游戏服设计

简单Elixir游戏服设计

来源:http://www.sketchydesignstudio.com 作者:龙8官网long8 时间:2019-10-05 15:59

修改player_server.ex

defmodule GameServer do
  use Application
  def start(_type, _args) do
    children = [
      {Registry, keys: :unique, name: PlayerRegistry},
      PlayerSupervisor
    ]
    Supervisor.start_link(children, strategy: :one_for_one)
  end
end

当乐游戏中心 1当乐游戏中心 2

  def application do
    [
      extra_applications: [:logger],
      mod: {GameServer, []}
    ]
  end
defmodule PlayerServer do
    use GenServer, restart: :temporary, start: {__MODULE__, :start_link, []}

    def start_link(player) do
        GenServer.start_link(__MODULE__, player, name: register_name(player))
      end

    def init(player) do
        {:ok, player}
    end

    def register_name(%{} = player), do: register_name(player |> Player.get_id)
    def register_name(id), do: {:via, Registry, {PlayerRegistry, id}}
end

修改game_server 项目标mix.exs, 扩大应用运行

defmodule PlayerSupervisorTest do
    use ExUnit.Case
    doctest PlayerSupervisor

    setup do
        Application.stop(GameServer)
        Application.start(GameServer)
        %{}
    end

    test "测试PlayerSupervisor启动PlayerServer" do
        assert {:ok, p1} = PlayerSupervisor.start_player(Player.init |> Player.set_id(1))
        assert {:ok, p2} = PlayerSupervisor.start_player(Player.init |> Player.set_id(2))
        assert [{^p1, nil}] = Registry.lookup(PlayerRegistry, 1)
        assert [{^p2, nil}] = Registry.lookup(PlayerRegistry, 2)
    end

end

当乐游戏中心,player_server.ex

修改game_server.ex 达成,如下应用的监察树监管PlayerRegistry进度和PlayerSupervisor。这里能够思索:one_for_rest 选项,因为PlayerRegistry 挂了, 只怕系统都玩不下来。

当乐游戏中心 3当乐游戏中心 4

修改测量检验

上回说用Registry 做地点注册(跨过服务器可以用syn,只是稍微麻烦点,供给效法global注册机制,写个包裹模块)。

player_supervisor_test.exs

 

本文由龙8官网long8发布于当乐游戏中心,转载请注明出处:简单Elixir游戏服设计

关键词:

上一篇:没有了

下一篇:话说晋城与晋东南