仅将用户连接到 ActionCable

Only connect users to ActionCable

我在我的应用程序上使用 ActinCable,但我遇到了授权问题。目前,actioncable 尝试授权网站上的每一个人,并反复进行授权。

这 returns 源源不断 An unauthorized connection attempt was rejected 在我的日志中。这是因为未登录的访问者也试图获得访问权限。

我的 connection.rb 看起来像这样:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

      def find_verified_user
        if current_user = User.find_by(id: cookies.signed[:user_id])
          current_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

现在我想知道是否可以让只有登录的人尝试获得 connnection.rb 的授权,而不是每个访问者使用该网站。我对 ActionCable 太陌生了,不知道如何限制它 - ActionCable 的文档仍处于早期阶段。

连接尝试是在调用 ActionCable.createConsumer() 时。您应该尝试仅在用户登录时调用它。