# app/channels/application_cable/connection.rb
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.encrypted[:user_id])
verified_user
else
reject_unauthorized_connection
end
end
end
end
identified_by 方法:标注一个key作为连接的id,可以用它再次找到特定的连接。通常是current_user, current_account,但不能瞎设置。在channel里有个数组存储这个identifiers,连上后就会加入到这个数组里面。 Note that anything marked as an identifier will automatically create a delegate by the same name on any channel instances created off the connection.这个 created off the connection, 我感觉意思是,离开了connection后创建,