我正在尝试从 JS 应用程序连接到 Phoenix/Elixir 中的 websocket 连接,但是,现在无论我尝试什么,我得到了
错误:
[info] GET /socket/websocket
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /socket/websocket (AirtameZap.Router)
从 Javascript 客户端,我通过以下方式连接:
socket = new Socket("ws://example/socket", {});
socket.connect();
socket.channel("zap:all")
...
以下是后端服务的源文件:
/lib/airtame_zap/endpoint.ex
defmodule AirtameZap.Endpoint do
use Phoenix.Endpoint, otp_app: :airtame_zap
socket "/socket", AirtameZap.ZapSocket
...
/web/channels/zap_socket.ex
defmodule AirtameZap.ZapSocket do
use Phoenix.Socket
channel "zap:*", AirtameZap.ZapChannel
transport :websocket, Phoenix.Transports.WebSocket
...
def connect(_params, socket) do
{:ok, socket}
end
def id(_socket), do: nil
end
/web/channels/zap_channel.ex
defmodule AirtameZap.ZapChannel do
use Phoenix.Channel
def join("zap:all", _message, socket) do
{:ok, socket}
end
end
有任何想法吗?我找到了这个回复Phoenix: Trying to connect to Channel but getting a not Route found for GET /websocket 错误,但没有帮助