【问题标题】:random extra websocket connection to phoenix app?随机额外的 websocket 连接到凤凰应用程序?
【发布时间】:2020-03-15 16:23:00
【问题描述】:

具有单通道的简单用户 websocket。我几乎从凤凰指南中的“如何”部分复制了这个代码字。

第一个请求是我的 - 它包含来自 facebook 登录响应的用户身份验证令牌。 如您所见,这来自phoenix.js 文件,它工作得很好......我能够发送和接收消息 - 没问题。

第二个似乎完全来自其他地方,我不知道为什么!?

frame.js 不是我的文件,所以必须是某种 node_module 依赖的一部分,js 被压缩成一行,不完全清晰。

我也每隔 5 秒左右在日志中得到这个:

phoenix_1  | [info] CONNECT GametimeWeb.UserSocket
phoenix_1  |   Transport: :websocket
phoenix_1  |   Connect Info: %{}
phoenix_1  |   Parameters: %{"token" => "", "vsn" => "2.0.0"}
phoenix_1  | :invalid - this is the response the socket returns I have jsut inspected it and printed to logs.
phoenix_1  | [debug] invalid
phoenix_1  | [info] Replied GametimeWeb.UserSocket :error

我在这里做错了什么?

凤凰 1.4.10

用户套接字:

defmodule GametimeWeb.UserSocket do
  use Phoenix.Socket

  require Logger

  ## Channels
  channel "sports:*", GametimeWeb.SportsChannel

  # Socket params are passed from the client and can
  # be used to verify and authenticate a user. After
  # verification, you can put default assigns into
  # the socket that will be set for all channels, ie
  #
  #     {:ok, assign(socket, :user_id, verified_user_id)}
  #
  # To deny connection, return `:error`.
  #

  def connect(%{"token" => token}, socket) do
    # max_age: 1209600 is equivalent to two weeks in seconds
    case Phoenix.Token.verify(socket, "user socket salt", token, max_age: 1209600) do
      {:ok, user_id} ->
        {:ok, assign(socket, :user, user_id)}
      {:error, reason} ->
        Logger.debug IO.inspect(reason)
        :error
    end
  end

端点代码:

defmodule GametimeWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :gametime

  socket "/socket", GametimeWeb.UserSocket,
    websocket: true, # or list of options
    longpoll: false

【问题讨论】:

    标签: websocket elixir phoenix-framework cowboy phoenix-channels


    【解决方案1】:

    这是实时重新加载功能的辅助套接字。当MIX_ENVprod 时不会打开。

    【讨论】:

      猜你喜欢
      • 2017-05-20
      • 2017-02-02
      • 2018-04-04
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2018-02-03
      • 2016-11-03
      • 2022-01-24
      相关资源
      最近更新 更多