【发布时间】:2016-11-14 01:20:12
【问题描述】:
我正在尝试使用 Channels (Websockets) 将 Angular 2 前端连接到 Phoenix 应用程序。它们完全独立并在 localhost 上的不同端口上运行(凤凰在 4000 上,角度 2 在 5555 上)。奇怪的是,我在后端收到 (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();
有人知道怎么回事吗?
【问题讨论】: