为什么 ActionCable 在找不到登录用户时不断尝试升级连接
Why does ActionCable continually try to upgrade a connection when it can't find a logged in user
Rails 5.2.1
路线:
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
.
.
.
我在 app/channels/application_cable/connection.rb
中有以下代码
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
if verified_user = env['warden'].user
verified_user
else
logger.add_tags 'ActionCable', "The user is not found. Connection rejected."
reject_unauthorized_connection
end
end
end
end
我注意到当我没有登录用户时,服务器日志如下所示:
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:38:11 +0700
ActionCable 不断尝试建立连接直到找到登录用户是否正确且正常?
据我所知,升级不是因为用户未登录。我相信这是 WS(S) 连接尝试通过 HTTP(S) 进行握手然后升级自身的功能。
这是您的 Web 服务器层(Puma、Apache 等)的行为,而不是您的应用程序层(Rails、Sinatra 等)的行为。
Rails 5.2.1
路线:
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
.
.
.
我在 app/channels/application_cable/connection.rb
中有以下代码module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
if verified_user = env['warden'].user
verified_user
else
logger.add_tags 'ActionCable', "The user is not found. Connection rejected."
reject_unauthorized_connection
end
end
end
end
我注意到当我没有登录用户时,服务器日志如下所示:
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:38:11 +0700
ActionCable 不断尝试建立连接直到找到登录用户是否正确且正常?
据我所知,升级不是因为用户未登录。我相信这是 WS(S) 连接尝试通过 HTTP(S) 进行握手然后升级自身的功能。
这是您的 Web 服务器层(Puma、Apache 等)的行为,而不是您的应用程序层(Rails、Sinatra 等)的行为。