可以安装多条 ActionCable 电缆吗? [英] Possible to mount multiple ActionCable cables?
本文介绍了可以安装多条 ActionCable 电缆吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以安装多个ActionCablecables相同的 Rails 应用程序?例如:
Is it possible to mount multiple ActionCable cables in the same Rails application? For example as such:
#routes.rb
Rails.application.routes.draw do
...
mount ActionCable.server => '/cable'
mount ActionCable.server => '/cable2'
end
我知道我可以使用相同的电缆拥有多个频道,但我需要对我的频道使用不同的身份验证方法.根据我的理解,使用同一根电缆是不可能的.
I am aware I can have multiple channels using the same cable, but I require to use different authentication methods for my channels. From my understanding, this is not possible using the same cable.
感谢您的帮助.
推荐答案
如 将 ActionCable 与多种识别方法结合使用,在同一个 Rails 应用中使用不同身份验证方法的一种方法可以是:
As elaborated in Using ActionCable with multiple identification methods, one approach to use different authentication methods in the same Rails app can be:
# app/channels/application_cable/connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user, :uuid
def connect
self.uuid = SecureRandom.urlsafe_base64
if env['warden'].user
self.current_user = find_verified_user
end
end
protected
def find_verified_user
return User.find_by(id: cookies.signed['user.id'])
end
end
end
经过身份验证的频道:
class AuthenticatedChannel < ApplicationCable::Channel
def subscribed
reject and return if current_user.blank?
stream_for current_user
end
...
匿名频道:
class AnonymousChannel < ApplicationCable::Channel
def subscribed
stream_from "channel_#{self.uuid}"
end
...
这篇关于可以安装多条 ActionCable 电缆吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文