具有单通道的简单用户 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