【问题标题】:Can I select particular processes/sockets tracked via Phoenix Presence?我可以选择通过 Phoenix Presence 跟踪的特定进程/套接字吗?
【发布时间】:2019-05-03 02:39:57
【问题描述】:

我的用例是让多个用户连接到大厅/候诊室,但只会从大厅中挑选其中两个开始对话,之后应将他们从大厅中删除。我该如何实施?请注意,在此系统中,用户没有注册,也没有用户名。他们应该直接从网页进来。

显然trackuntrack 函数也有接受pid 作为参数的变体。但是,当需要开始对话时,我不确定如何首先检索进程的 pid。

函数self() 在这种情况下使用正确吗?也就是说,也许我可以写

def handle_info(:after_lobby_join, socket) do
  Presence.track(socket, "lobby", %{
    pid: self()
  })

  {:noreply, socket}
end

def handle_info(:start, socket) do
  pid1 = hd(Presence.list(socket)["lobby"][:metas])[:pid]
  # Start the conversation by sending messages individually to pid1 and pid2
  ...
  untrack(pid1, "my_app:lobby", "lobby")
  {:ok, socket}
end

或者我是否使问题过于复杂/没有正确理解 Presence?

还有一个 phx_ref 字段,但我似乎无法将其用于此目的。

另外,显然我只想向被选中的两个用户发送"start_conversation" 消息,而不是大厅中的其他用户。我看到函数push 将消息发送到指定的套接字。但是如果我在跟踪 pid,是否可以从 pid 中识别出相应的套接字?

【问题讨论】:

    标签: websocket elixir phoenix-framework user-presence


    【解决方案1】:

    我最终使用个人用户渠道解决了我最初的问题。我只是让前端生成随机用户 ID。

    提出了不使用用户频道的解决方案in Elixir forum。但它不使用 Presence。

    def handle_info({:new_user, socket}, [{us1, ref1}, {us2, ref2}]) do
      # we had 2 users, a new one joined, so that's 3
      user_sockets = [socket, us1, us2]
      # we can create a room for them now
      create_new_room(user_sockets)
      # and clean the state
      Enum.each([ref1, ref2], fn ref ->
        Process.demonitor(ref)
      end)
      {:noreply, []}
    end
    
    def handle_info({:new_user, socket}, waiting_user_sockets) do
      # otherwise just add the new users to the waiting users list
      ref = Process.monitor(socket)
      {:noreply, [{socket, ref} | waiting_user_sockets]}
    end
    
    def handle_info({:DOWN, ref, :process, _object, _reason}, waiting_user_sockets) do
      # remove the disconnected socket
    end
    

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      相关资源
      最近更新 更多