Elixir/Phoenix 在加入频道时传递负载

Elixir/Phoenix pass payload on joining channel

频道有authorized?功能,我想在加入频道时传递生成的本地令牌,以便我可以验证用户的角色,就像这样:

const data = { token: localStorage.getItem('phoenixAuthToken') };
channel.join(data).receive('ok', (response) => {
            ...
            });
        });

但是,在我的频道设置中,我似乎没有收到来自客户端的任何加入:

def join("settings", payload, socket) do
  IO.inspect(payload)
  if authorized?(payload) do
    {:ok, socket}
  else
    {:error, %{reason: "unauthorized"}}
  end
end

IO.inspect(payload) 就是 %{}。我在这里做错了什么?是不是加入频道也能收到消息?

join/3收到的payload是JS客户端的one set in the second argument of .channel(),不是传给.join()的参数。所以,在客户端,你应该做这样的事情:

const data = { token: localStorage.getItem('phoenixAuthToken') };
const channel = socket.channel("foo", data);
channel.join().receive(...);