use*_*795 1 elixir websocket cowboy phoenix-framework phoenix-channels
具有单个通道的简单用户 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
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
凤凰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
Run Code Online (Sandbox Code Playgroud)
端点代码:
defmodule GametimeWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :gametime
socket "/socket", GametimeWeb.UserSocket,
websocket: true, # or list of options
longpoll: false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
212 次 |
| 最近记录: |