From: Christopher Coté Date: Fri, 4 Dec 2020 04:20:22 +0000 (-0600) Subject: make sure to send connected message to processes that add themselves after we've... X-Git-Url: http://git.entropealabs.com/?a=commitdiff_plain;h=11f883eeda58391073d692e54cf997f7cf898e93;p=wampex_client.git make sure to send connected message to processes that add themselves after we've already connected --- diff --git a/lib/client.ex b/lib/client.ex index fada2f2..9255846 100644 --- a/lib/client.ex +++ b/lib/client.ex @@ -89,7 +89,13 @@ defmodule Wampex.Client do @spec add(name :: module(), pid :: module() | pid()) :: {:ok, pid()} | {:error, term()} def add(name, pid) do - Registry.register(dependent_registry_name(name), pid, pid) + case session_exists(name) do + false -> + %Error{error: "wamp.error.no_session"} + + sess_name -> + Sess.add(sess_name, pid) + end end @spec remove(name :: module(), pid :: module() | pid()) :: :ok diff --git a/lib/client/session.ex b/lib/client/session.ex index c5e0e9f..1ced226 100644 --- a/lib/client/session.ex +++ b/lib/client/session.ex @@ -39,6 +39,7 @@ defmodule Wampex.Client.Session do :challenge, :interrupt, message_queue: [], + connected: false, request_id: 0, protocol: "wamp.2.msgpack", transport: WebSocket, @@ -61,6 +62,7 @@ defmodule Wampex.Client.Session do challenge: Challenge.t() | nil, interrupt: integer() | nil, message_queue: [], + connected: boolean(), request_id: integer(), protocol: binary(), transport: module(), @@ -110,6 +112,24 @@ defmodule Wampex.Client.Session do __MODULE__.call(name, {:send_request, request}, timeout) end + def add(name, pid) do + __MODULE__.call(name, {:add, pid}) + end + + @impl true + def handle_call({:add, pid}, from, _, %SL{data: %Sess{name: name, connected: conn}} = sl) do + case conn do + true -> + send(pid, {:connected, name}) + + false -> + :noop + end + + r = Registry.register(Client.dependent_registry_name(name), pid, pid) + {:ok, sl, [{:reply, from, r}]} + end + @impl true def handle_call( {:send_request, request}, @@ -228,7 +248,7 @@ defmodule Wampex.Client.Session do @handle_welcome, _, @welcome, - %SL{data: %Sess{name: name}} = sl + %SL{data: %Sess{name: name} = session} = sl ) do deps = Registry.select(Client.dependent_registry_name(name), [{{:"$1", :_, :_}, [], [:"$1"]}]) @@ -236,7 +256,8 @@ defmodule Wampex.Client.Session do send(pid, {:connected, name}) end) - {:ok, sl, [{:next_event, :internal, :established}]} + {:ok, %SL{sl | data: %Sess{session | connected: true}}, + [{:next_event, :internal, :established}]} end @impl true