[英]how to create action cable connection in rails
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags "ActionCable","User #{current_user.id}"
end
def find_varified_user
if current_user = env['wardon'].user
current_user
else
reject_unauthorized_connection
end
end
end
end
首先,您生成一个频道
rails g channel {channel_name} {the action you will send to}
这将在“app/channels”文件夹中生成频道
然后在 routes.rb 中挂载频道
#config/routes.rb
# add this line
mount ActionCable.server => '/cable'
然后您从客户端应用程序订阅频道并使用您在频道中创建的方法
您可以查看本教程使用 Action Cable 创建聊天
但忽略客户端部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.