在Phoenix中无法连接到Web套接字:忽略不匹配的主题。但我认为它匹配

时间:2018-08-05 16:50:12

标签: websocket elixir phoenix

我是Phoenix和Web套接字的新手。我正在尝试使用Web套接字创建一个应用程序,但是Phoenix说Ignoring unmatched topic "analysis:dn-ds.axin2" in Protsci.UserSocket。我认为我已经为此类主题找到了合适的处理程序,所以我不明白问题出在哪里。我是否错过了Phoenix应用程序中的某些内容?还是可以在客户端上?这是我的设置:

# web/ |
# -----| user_socket.ex
# -----| analysis_channel.ex

# user_socket.ex
defmodule Protsci.UserSocket do
  use Phoenix.Socket

  channel "analysis:*", Protsci.AnalysisChannel

  transport :websocket, Phoenix.Transports.WebSocket

  def connect(_params, socket) do
    {:ok, socket}
  end

  def id(_socket), do: nil
end

# analysis_channel.ex
defmodule Protsci.AnalysisChannel do
  use Phoenix.Channel
  require Logger

  def join("analysis:" <> topic, _message, socket) do
    Logger.info "Join"
    case String.split(topic, ".") do
      [analysis, protein] ->
        reply = %{ :received => :message, :analysis => analysis, :protein => protein }
        {:ok, reply, socket}
      _ ->
        reply = %{ :error => "Unmatched topic", :msg => :topic }
        {:ok, reply, socket}
    end

  end

  def handle_in(
    analysis,
    %{"x" => x, "y" => y, "z" => z},
    socket
    ) do
    Logger.info "in"
    broadcast! socket, :analysis, %{body: x + y + z}
    {:noreply, socket}
  end

  def handle_in("new:msg", _msg, socket) do
    broadcast! socket, :type, %{body: "Unknow message type"}
    {:noreply, socket}
  end

  def handle_in("analysis:" <> topic, _msg, socket) do
    broadcast! socket, topic, %{body: "Unknow message type"}
    {:noreply, socket}
  end

end

1 个答案:

答案 0 :(得分:3)

问题是由于我在发送消息之前没有加入频道。