gpt4 book ai didi

elixir - Phoenix : Trying to connect to Channel but getting a not Route found for GET/websocket error

转载 作者:行者123 更新时间:2023-12-04 10:35:38 28 4
gpt4 key购买 nike

我正在尝试使用Channels(Websockets)将Angular 2前端连接到Phoenix应用程序。它们是完全分开的,并且可以在本地主机上的不同端口上运行(4000上的phoenix,5555上的angular 2)。奇怪的是,我在后端收到(Phoenix.Router.NoRouteError) no route found for GET /websocket (MyApp.Router)错误。前端出现代码1006错误:
WebSocket connection to 'ws://localhost:4000/websocket?vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 404
// endpoint.ex

defmodule MyApp.Endpoint do
use Phoenix.Endpoint, otp_app: :my_app

# socket "/socket", MyApp.UserSocket
socket "/websocket", MyApp.PostSocket

# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production.
plug Plug.Static,
at: "/", from: :my_app, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
end

plug Plug.RequestId
plug Plug.Logger

plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison

plug Plug.MethodOverride
plug Plug.Head

plug Plug.Session,
store: :cookie,
key: "_my_app_key",
signing_salt: "qCSHk+9O"

plug CORSPlug, [
origin: "http://localhost:5555",
headers: ["X-Auth-Token" | CORSPlug.defaults[:headers]]
]

plug MyApp.Router
end
PostSocket:
defmodule MyApp.PostSocket do
use Phoenix.Socket

## Channels
channel "post:*", MyApp.PostChannel

## Transports
transport :websocket, Phoenix.Transports.WebSocket
transport :longpoll, Phoenix.Transports.LongPoll


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

def id(_socket), do: nil
end

在前端,使用phoenix js客户端:
var socket = new Socket("ws://localhost:4000", {
logger: ((kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }),
transport: WebSocket
});
socket.connect();

有人知道发生了什么吗?

最佳答案

This question的JoséValim回答了正确的方向。

the suffix of the path should be whatever the transport layer is. In this case, it needed /websocket at the end in the implementation.



因此,在我的端点中,我将路由从 /websocket更改为 /socket以防止混淆:
defmodule TropeApi.Endpoint do
use Phoenix.Endpoint, otp_app: :trope_api

socket "/socket", TropeApi.PostSocket

# ...

end

然后更改js实现以反射(reflect)这一点。我也将transport参数添加到了选项中(只是要提一下,这与它有任何关系,我认为这不是)。
var socket = new Socket("ws://localhost:4000/socket", {
logger: ((kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }),
transport: WebSocket
});
socket.connect();

现在,连接转到 ws://localhost:4000/socket/websocket。最后一部分( websocket)让我有些困惑,但是现在已经清除了。

关于elixir - Phoenix : Trying to connect to Channel but getting a not Route found for GET/websocket error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38318040/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com