我正在使用ActionCable进行多租户应用程序。我通过以下方式验证我的连接:
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', "User #{current_user.id}"
end
protected
def find_verified_user
# pry.binding
if current_user = User.find_by(subdomain: request.subdomain)
current_user
else
reject_unauthorized_connection
end
end
首次请求连接正常:
Started GET "/cable" for 127.0.0.1 at 2017-01-04 17:57:41 +0000
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-01-04 17:57:41 +0000
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
User Load (0.7ms) SELECT "public"."users".* FROM "public"."users" WHERE "public"."users"."subdomain" = $1 LIMIT $2 [["subdomain", "kanyi"], ["LIMIT", 1]]
[ActionCable] [User 1] Registered connection (Z2lkOi8vYmx1ZW9yaWdpbnMvVXNlci8x)
[ActionCable] [User 1] Chatroom Load (1.0ms) SELECT "chatrooms".* FROM "chatrooms"
[ActionCable] [User 1] ChatroomsChannel is transmitting the subscription confirmation
Started GET "/cable" for ::1 at 2017-01-04 17:58:03 +0000
Started GET "/cable/" [WebSocket] for ::1 at 2017-01-04 17:58:03 +0000
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
但在第二次尝试时,动作电缆拒绝连接;
User Load (2.2ms) SELECT "public"."users".* FROM "public"."users" WHERE "public"."users"."subdomain" = $1 LIMIT $2 [["subdomain", ""], ["LIMIT", 1]]
An unauthorized connection attempt was rejected
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Finished "/cable/" [WebSocket] for ::1 at 2017-01-04 17:58:03 +0000
Finished "/cable/" [WebSocket] for ::1 at 2017-01-04 17:58:03 +0000
是什么导致第二个请求失败?
问题1:可操作的HTTP升级请求:
需要NGINX配置的分辨率更改为接受此操作电缆请求。
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
将以上行添加到nginx站点配置中的位置块,然后重新启动nginx。这个问题应该解决。
链接:resource
问题2:未经授权的连接尝试
我回到你的下一个问题,即“未经授权的连接尝试被拒绝”
此问题驻留在您的'connection.rb'文件中。您需要检查授权用户如何使用操作电缆。检查'connection.rb'中的函数“find_verified_user”。
特别是这条线"User.find_by(subdomain: request.subdomain)"
不起作用。
希望能帮助到你,
谢谢