【问题标题】:Elixir Phoenix Channels长生不老药凤凰频道
【发布时间】:2021-01-29 11:07:47
【问题描述】:

我是 elixir 和 phoenix 的新手,并尝试使用 phoenix 后端和 js 前端来设置频道。我设置了频道,但收到一个反复发生的 Ranch 侦听器错误(一遍又一遍地重复相同的错误循环)。

[info] CONNECTED TO SenseiWeb.UserSocket in 138µs
  Transport: :websocket
  Serializer: Phoenix.Socket.V2.JSONSerializer
  Parameters: %{"vsn" => "2.0.0"}
[error] Ranch listener SenseiWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.519.0> exit with reason: {:undef, [{SenseiWeb.SymbolChannel, :child_spec, [{SenseiWeb.Endpoint, {#PID<0.519.0>, #Reference<0.1115078885.25165827.139445>}}], []}, {Phoenix.Channel.Server, :join, 4, [file: 'lib/phoenix/channel/server.ex', line: 25]}, {Phoenix.Socket, :handle_in, 4, [file: 'lib/phoenix/socket.ex', line: 617]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 175]}, {:cowboy_websocket, :handler_call, 6, [file: '/Users/ndshah82/projects/sensei-trader-elixir/deps/cowboy/src/cowboy_websocket.erl', line: 528]}, {:cowboy_http, :loop, 1, [file: '/Users/ndshah82/projects/sensei-trader-elixir/deps/cowboy/src/cowboy_http.erl', line: 254]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 226]}]}
defmodule SenseiWeb.UserSocket do
  use Phoenix.Socket

  channel "symbols:*", SenseiWeb.SymbolChannel
  @impl true
  def connect(_params, socket, _connect_info) do
    {:ok, assign(socket, :user_id, 1)}
  end

  @impl true
  def id(socket), do: "users_socket:#{socket.assigns.user_id}"
end
defmodule SenseiWeb.SymbolChartsChannel do
  use SenseiWeb, :channel

  def join("symbols:charts", _params, socket) do
    {:ok, socket}
  end

  def handle_info("update_symbols", socket) do
    push(socket, "ping", %{count: 1})
    { :noreply, assign(socket, :count, 1) }
  end

  def handle_in("update_symbols", _, socket) do
    push(socket, "update_symbols", %{val: 1})
    {:noreply, socket}
  end
end
const Symbol = {
  init(socket, addSymbolButton) {
    if(!addSymbolButton) {
      return
    }

    const channel = socket.channel("symbols:charts", {abc: 1})

    channel.on("update_symbols", resp => console.log(resp))

    channel.join()
      .receive("ok", ({messages}) => console.log("catching up", messages) )
      .receive("error", ({reason}) => console.log("failed join", reason) )
      .receive("timeout", () => console.log("Networking issue. Still waiting..."))
}

【问题讨论】:

    标签: javascript elixir phoenix-framework


    【解决方案1】:

    在你的SenseiWeb.UserSocket,声明

    channel "symbols:*", SenseiWeb.SymbolChannel
    

    应该是

    channel "symbols:*", SenseiWeb.SymbolChartsChannel
    

    顺便说一下错误原因

    {:undef, [{SenseiWeb.SymbolChannel, :child_spec, ...}]}
    

    告诉你“模块”SenseiWeb.SymbolChannel(它只是一个原子:"Elixir.SenseiWeb.SymbolChannel")没有名为:child_spec 的函数。要么您忘记在频道模块中添加行 use SenseiWeb, :channel(这不是您的情况),要么 SenseiWeb.SymbolChannel 本身不是模块。

    【讨论】:

    • 非常感谢!我从头到尾都看不到它。
    • @NishantShah 很高兴能帮上忙 :)
    猜你喜欢
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    相关资源
    最近更新 更多