问题描述
我是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,[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,:count,1) }
end
def handle_in("update_symbols",_,"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
的功能。您要么忘记在频道模块中添加行use SenseiWeb,:channel
(不是您的情况),要么SenseiWeb.SymbolChannel
本身不是模块。