套接字不会连接到端点

Socket won't connect to Endpoint

var socket = new Socket("localhost:4000")
socket.connect()

Returns WebSocket connection to 'ws://localhost:4000/ws' failed: Error during WebSocket handshake: Unexpected response code: 404

但是我在 /ws 端点上确实有套接字,对吗?

defmodule Sapphire.Endpoint do
  use Phoenix.Endpoint, otp_app: :sapphire

  socket "/ws", Sapphire.MomentSocket

  plug Plug.Static,
    at: "/", from: :sapphire, gzip: false,
    only: ~w(css fonts images js favicon.ico robots.txt)

  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: "_sapphire_key",
    signing_salt: "hW1bFEcR"

  plug Sapphire.Router

end

它应该能够连接到那个端点,但由于某种原因它根本无法连接到它。

[info] Running Sapphire.Endpoint with Cowboy on http://localhost:4000

@JoséValim 找到了解决方案。

我正在将 phoenix.js 库移植到 coffeescript,但忽略了路径的后缀应该是传输层的事实。在这种情况下,它需要 /websocket 在实施的最后。 :)

完全公开 -- 我是一个菜鸟,我可能搞砸了一些事情,但这就是我回到正轨的方式。

rm -rf deps/phoenix

这将清除您当前版本的 phoenix(包括 phoenix.js)

mix do deps.get

这又把凤凰拉下来了

希望这足以让你在这一点上再次前进——但如果你像我一样搞砸了迁移,并且你在 mac/linux,请执行以下操作:

find . -name phoenix.js

瞧,Jose 修复了 phoenix.js。我很懒,只是复制了那个 phoenix.js 暂时让事情顺利进行。