套接字不会连接到端点

时间:2015-09-19 07:13:19

标签: elixir phoenix-framework

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

返回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

1 个答案:

答案 0 :(得分:5)

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

我正在将phoenix.js库移植到coffeescript,并错过了路径的后缀应该是传输层的事实。在这种情况下,它在实现结束时需要/websocket。 :)