Ruby on rails 操作电缆重新连接失败
我将ActionCable用于多租户应用程序。 我通过以下方式验证我的连接:Ruby on rails 操作电缆重新连接失败,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我将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_us
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)
但在第二次尝试中,action cable拒绝了连接
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。这个问题应该得到解决
链接:
问题2:未经授权的连接尝试
我将回到您的下一个问题,即“未经授权的连接尝试被拒绝”
此问题存在于您的'connection.rb'文件中。您需要检查如何授权用户使用action cable。在'connection.rb'中检查功能“查找已验证的用户”
具体来说,这一行“User.find_by(subdomain:request.subdomain)”不起作用
希望有帮助
谢谢问题1:可操作的HTTP升级请求: 需要NGINX配置的解决方案将更改以接受此操作电缆请求
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
将上述行添加到nginx站点配置中的位置块,然后重新启动nginx。这个问题应该得到解决
链接:
问题2:未经授权的连接尝试
我将回到您的下一个问题,即“未经授权的连接尝试被拒绝”
此问题存在于您的'connection.rb'文件中。您需要检查如何授权用户使用action cable。在'connection.rb'中检查功能“查找已验证的用户”
具体来说,这一行“User.find_by(subdomain:request.subdomain)”不起作用
希望有帮助
谢谢从日志来看,第二个请求的子域似乎没有设置为“kanyi”。从日志来看,第二个请求的子域似乎没有设置为“kanyi”。