1
我有一个应用程序,2人聊天。但他们聊天之前,双方必须按“接受”Phoenix:如何存储通道中的套接字共享的状态?
在前端我有一个按钮,调用该函数:我在凤凰路
function acceptMatch() {
channel.push("accept_match", {
"matched_client_email": matchedClientEmail,
});
}
我有这样的(这是不完整的,因为我也不太清楚该怎么做):
def handle_in("accept_match", %{ "matched_client_email" => matched_client_email }, socket) do
Logger.info "accept_match"
...
############################### here ####################################
match_accepted = true # <-- I want to be able to find out if the other person has pressed "accept" as well
case match_accepted do
true ->
...
{:noreply, socket}
_ ->
{:noreply, socket}
end
end
我看着Agent
,但似乎被捆绑到一个PID,我将无法轻松地来回传递到不同的插座。我试图在所有可以保存/查询的连接之间寻找一个通用存储(除了数据库)。这样,如果甲方按下接受,我可以问存储“乙方是否接受?”
=== UPDATE ===
当我尝试使用一个名为代理我得到这个:
iex(11)> myfunc = fn -> %{} end
#Function<20.52032458/0 in :erl_eval.expr/5>
iex(12)> Agent.start_link(myfunc, name: "Cool")
** (CaseClauseError) no case clause matching: {"Cool", []}
(elixir) lib/gen_server.ex:544: GenServer.do_start/4
iex(12)>
ooo cool!当我尝试这样做时,我得到一个错误...更新我的问题与错误 – Edmund
看起来像它的作品,当它是一个原子! 'name::cool' – Edmund
是的,对不起。我更新了我的答案。名称必须符合https://hexdocs.pm/elixir/GenServer.html#module-name-registration中的要求 – PatNowak