【问题标题】:Presence not picking up user leave events?Presence 不接收用户离开事件?
【发布时间】:2019-05-27 22:08:10
【问题描述】:

当用户离开频道时我需要执行一些操作(在大多数情况下,他们自愿关闭标签页,但也可能出现连接丢失/超时等)

根据https://elixirforum.com/t/phoenix-presence-run-some-code-when-user-leaves-the-channel/17739How to detect if a user left a Phoenix channel due to a network disconnect?之类的帖子,从Presence拦截"presence_diff"事件似乎是一种万无一失的方法,因为它也应该涵盖连接异常终止的情况。

奇怪的是,presence_diff 事件似乎只在我通过Presence.track 跟踪用户时触发,而不是在用户离开时触发。

同时,在我的频道中添加 terminate(reason, socket) 回调可以正确捕获离开事件。

我想知道我的配置有什么问题。还是我没有正确理解 Presence 的用法?

示例代码:

def join("participant:" <> participant_id, _payload, socket) do
  if socket.assigns.participant_id == participant_id do
    send(self(), :after_participant_join)
    {:ok, socket}
  else
    {:error, %{reason: "unauthorized"}}
  end
end

def handle_info(:after_participant_join, socket) do
  experiment_id = socket.assigns.experiment_id

  Presence.track(socket, experiment_id, %{
    # keys to track
  })

  # Broadcast something
  # broadcast(socket, ...)

  {:noreply, socket}
end

intercept(["presence_diff"])

def handle_out("presence_diff", payload, socket) do
  # Only gets triggered at Presence.track, but not when the connection is closed.
  IO.puts("presence_diff triggered, payload is #{inspect(payload)}")

  leaves = payload.leaves

  for {experiment_id, meta} <- leaves do
    IO.puts("Leave information: #{meta}")

    # Do stuffs
    end
end

# This works, however.
def terminate(reason, socket) do
  IO.puts("terminated. #{inspect(reason)}")

  # Do stuffs.
end

【问题讨论】:

    标签: websocket elixir phoenix-framework


    【解决方案1】:

    好的,我想我知道发生了什么:每个"participant:" &lt;&gt; participant_id 主题,顾名思义,只有一个参与者订阅。因此,当该参与者退出时,进程也会终止,并且没有人能够对 presence_diff 消息采取行动。

    仍然需要一个单独的过程。可以从该进程调用MyApp.Endpoint.subscribe 订阅"participant:" &lt;&gt; participant_id 主题并对presence_diff 消息采取行动。

    或者可以设置一台外接显示器。见How to detect if a user left a Phoenix channel due to a network disconnect?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2012-06-16
      • 2023-03-08
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多