广播到凤凰城1.1.6的不同频道



我正试图在我的应用程序中向另一个频道广播,但我无法使其工作。我也试着写一个测试,但我不知道怎么写。

据我所知,我成功地从notification_channel广播了消息,但在chat_channel中没有收到。

通知应该发送到聊天室。

notification_channel.ex

  def handle_in("new:group:recommendation", msg, socket) do
    payload = %{
        message: msg["message"],
        url: msg["url"],
        title: msg["title"],
        user_name: get_name_of_user(socket.assigns.user_grapqhl_id),
        user_grapqhl_id: socket.assigns.user_grapqhl_id
    }
    IO.puts "incomming"
    IO.inspect msg
    Enum.map(msg["groups"], fn(x) ->
        App.Endpoint.broadcast_from! self(), "chat:"<>x,
            "new:recommendation", payload
        end)
    {:reply, :ok, socket}
  end

chat_channel.ex

 def handle_in("new:recommendation", msg, socket) do
      IO.puts "i am a recommendation !"
      IO.inspect msg
      chat_msg = %{
         "creator_id" => msg["user_grapqhl_id"],
         "text" => msg["message"],
         "creator_name" => msg["user_name"]
      }
     broadcast! socket, "new:msg", create_chat_msg(chat_msg,socket)
     {:reply, :ok, socket}
  end

测试

  test "do we send a new:recommendation to chat ?", %{guardian_token: guardian_token} do
      nils_base_64 = Base.encode64("user:nils")
      {:ok, socket} = connect(UserSocket, %{})
      {:ok, _, socket1} = subscribe_and_join(socket, "notifications:"<>nils_base_64, %{"guardian_token" => guardian_token})
      {:ok, _, socket} = subscribe_and_join(socket1, "chat:Y2hhdDpjaGF0Mw==", %{"guardian_token" => guardian_token})
      payload = %{
          "message" => "look at this cool thing!",
          "url" => "link to stuff",
          "title" => "AWESOME EVENT",
          "groups" => ["Y2hhdDpjaGF0Mw==", "Y2hhdDpwdWJsaWM="]
      }
      reply = %{message: "look at this cool thing!", title: "AWESOME EVENT", url: "link to stuff", user_grapqhl_id: nils_base_64, user_name: "Nils Eriksson"}
      ref = push socket1, "new:group:recommendation", payload
      assert_reply ref, :ok
      assert_broadcast "new:recommendation", ^reply
  end

这个测试通过了,我可以通过更改reply来使它失败或者评论广播。我不能通过在chat_channel中将handle_in更改为接收fail:please来使其失败。如果我寄零钱给它,它会抱怨的ref = push socket1, "new:group:recommendation", payload在这种情况下不支持CCD_ 6。

这就是电线上的东西。

     Process mailbox:
   %Phoenix.Socket.Message{event: "init:msgs", payload: %{messages: []}, ref: nil, topic: "chat:Y2hhdDpjaGF0Mw=="}
   %Phoenix.Socket.Broadcast{event: "new:recommendation", payload: %{message: "look at this cool thing!", title: "AWESOME EVENTs", url: "link to stuff", user_grapqhl_id: "dXNlcjpuaWxz", user_name: "Nils Eriksson"}, topic: "chat:Y2hhdDpjaGF0Mw=="}
   %Phoenix.Socket.Message{event: "new:recommendation", payload: %{message: "look at this cool thing!", title: "AWESOME EVENTs", url: "link to stuff", user_grapqhl_id: "dXNlcjpuaWxz", user_name: "Nils Eriksson"}, ref: nil, topic: "chat:Y2hhdDpjaGF0Mw=="}

我使用通道身份验证,因为我使用的elm包还不支持套接字级别的身份验证。这就是chat 中的样子

  def join("chat:" <> chat_id, %{"guardian_token" => token}, socket) do
  IO.puts chat_id
  case sign_in(socket, token) do
     {:ok, authed_socket, _guardian_params} ->
         Process.flag(:trap_exit, true)
         send(self, {:after_join})
         [_type, node_chat_id] = Node.from_global_id(chat_id)
         {:ok, assign(authed_socket, :chat_id, node_chat_id)}
     {:error, reason} ->
         IO.puts "Can't join channel cuz: " <> reason
       # handle error TODO
   end

结束

因为您使用Endpoint中的broadcast_from/4。您应该在chat_channel:中使用handle_info/2

alias Phoenix.Socket.Broadcast
  ...
def handle_info(%Broadcast{topic: _, event: ev, payload: payload}, socket) do
    IO.puts ev
    IO.inspect payload
    # do something with ev and payload( push or broadcast)
    {:noreply, socket}
  end

或者你可以从你的客户那里收听该事件:

chatChannel.on("new:recommendation", resp => {
   // doSomething with response
}

编辑:

让我们来解释一下channelPubSub系统是如何工作的。

当您想要广播或推送具有有效负载的事件时。首先它将发送到PubSub系统,然后PubSub系统将它发送到具有channelPubSub系统注册的主题的所有订户进程(channel)。

当您使用Endpoint.broadcast_from/4从服务器广播事件时。PubSub系统将接收具有有效载荷的事件,并将该事件广播到该频道注册的主题。

通道将触发handle_out回调,并将消息推送给客户端。因此,在您的chat_channel中,您不需要handle_in"新:推荐"活动。你的客户只需要听听这个事件。

chatChannel.on("new:recommendation", resp => {
   // do something with response
}

让我重写你的测试:

setup do
    nils_base_64 = Base.encode64("user:nils")
    {:ok, socket} = connect(UserSocket, %{})
    {:ok, _, socket} = subscribe_and_join(socket, "notifications:"<>nils_base_64, %{"guardian_token" => guardian_token})
    {:ok, socket: socket}
  end

test "do we send a new:recommendation to chat ?", %{socket: socket} do
      MyApp.Endpoint.subscribe("chat:Y2hhdDpjaGF0Mw==")
      payload = %{
          "message" => "look at this cool thing!",
          "url" => "link to stuff",
          "title" => "AWESOME EVENT",
          "groups" => ["Y2hhdDpjaGF0Mw==", "Y2hhdDpwdWJsaWM="]
      }

      reply = %Phoenix.Socket.Broadcast{message: "look at this cool thing!",
              title: "AWESOME EVENT",
              url: "link to stuff",
              user_grapqhl_id: nils_base_64,
              user_name: "Nils Eriksson"}
      ref = push socket, "new:group:recommendation", payload
      assert_reply ref, :ok
      assert_receive ^reply
  end

通过subscribe到您想要收听的主题,您可以确保您的频道收到带有assert_receive的消息。这是将broadcast测试到不同信道的方法。

试试看,告诉我。测试会通过的。

使用App.Endpoint.broadcast topic, event, payload作为聊天频道的主题。它应该起作用。

相关内容

  • 没有找到相关文章

最新更新