带有子域的 rails 应用程序中的动作电缆 - 公寓 gem 无法正常工作
Action cable in rails application with subdomain - apartment gem isn't working
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 1 and Link 2 但不起作用。
在生产中:request.subdomain
是 nil
(尝试对一些租户值进行热编码,但不起作用)
开发中:request.subdomain
提供租户名称,但实际请求无效。
有什么帮助吗?
解决了这个问题:
request.subdomain
不工作并且 returns“”。或者我通过查询字符串
传递租户值
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
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 1 and Link 2 但不起作用。
在生产中:request.subdomain
是 nil
(尝试对一些租户值进行热编码,但不起作用)
开发中:request.subdomain
提供租户名称,但实际请求无效。
有什么帮助吗?
解决了这个问题:
request.subdomain
不工作并且 returns“”。或者我通过查询字符串
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