如何从内部检索会话存储 ActionCable::Connection::Base

How retrieve session store from inside ActionCable::Connection::Base

我从动作电缆文档中阅读了这个设置示例 current_user:

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 = User.find_by(id: cookies.signed[:user_id])
        verified_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

我没有设置 user_id cookie,因为身份验证是针对 cas 服务器进行的,它在 Active Record 中存储会话。 如何告诉 Connection class 当前用户?

您需要强制加载会话。在此处检查此 link(为我工作):How force that session is loaded?