动作电缆重新连接失败

时间:2017-01-04 18:04:41

标签: 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_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 个答案:

答案 0 :(得分:0)

问题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)"不起作用。

希望有帮助,

谢谢