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的正常行为?
答案 0 :(得分:0)
AFAIK,升级不是因为未登录用户。我相信这是WS(S)连接尝试通过HTTP(S)进行握手然后进行自我升级的功能。
这是您的Web服务器层(Puma,Apache等)的行为,而不是您的应用程序层(Rails,Sinatra等)的行为。