【问题标题】:Action cable in rails application with subdomain - apartment gem isn't working带有子域的 Rails 应用程序中的动作电缆 - 公寓 gem 不工作
【发布时间】:2020-12-21 05:25:16
【问题描述】:

Rails 5, apartment gem

动作线connection.rb

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

    def connect
      self.tenant = request.subdomain
      Apartment::Tenant.switch!(tenant)

      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.try(:email) || 'Unauthenticated User'
    end

    protected

    def find_verified_user # this checks whether a user is authenticated with devise
      if verified_user = env['warden'].user
        verified_user
      else
        # May have public users accessing the channels
        # reject_unauthorized_connection
      end
    end
  end
end

environments/production.rb

config.action_cable.allowed_request_origins = [ /https:\/\/(.*).mydomain.com/ ]

尝试了从这些链接中获取的上述代码参考:Link 1Link 2,但无法正常工作。

在生产中:request.subdomainnil(尝试对一些租户值进行热编码,但不起作用)

开发中:request.subdomain 提供租户名称,但实际请求不起作用。

有什么帮助吗?

【问题讨论】:

    标签: ruby-on-rails actioncable apartment-gem


    【解决方案1】:

    修复了这个问题:

    request.subdomain 不工作并返回“”。或者,我通过查询字符串传递了租户值

    const getWebSocketURL = () => {
        var protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
        var host = window.location.host;
        var path = '/cable';
        var url = protocol + host + path;
        return `${url}?tenant=mytenant`;
    };
    

    const wsUrl = getWebSocketURL();
    const cable = ActionCable.createConsumer(wsUrl);
    

    connection.rb:

    self.tenant = request.params[:tenant]
    

    在你的频道中,你的第一行应该是每个方法

    Apartment::Tenant.switch!(tenant)
    

    这里你可以从房间获取租户值

    也可以查看这些链接: how-to-set-tenant-in-actioncable
    rails-actioncable-request-origins-subdomain

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 2016-11-23
      • 2018-05-11
      相关资源
      最近更新 更多