我是 Elixir 和 phoenix 的新手,并尝试使用 phoenix 后端和 js 前端设置 channel 。我已经设置了 channel ,但收到重复发生的 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..."))
}
最佳答案
在您的SenseiWeb.UserSocket
中,声明
channel "symbols:*", SenseiWeb.SymbolChannel
应该是
channel "symbols:*", SenseiWeb.SymbolChartsChannel
顺便说一下错误原因
{:undef, [{SenseiWeb.SymbolChannel, :child_spec, ...}]}
告诉您“模块”SenseiWeb.SymbolChannel
(它只是一个原子:"Elixir.SenseiWeb.SymbolChannel"
)没有名为的函数>:child_spec
。您可能忘记在 channel 模块中添加 use SenseiWeb, :channel
行(这不是您的情况),或者 SenseiWeb.SymbolChannel
本身不是一个模块。
关于javascript - Elixir Phoenix channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64363541/