【问题标题】:Possible to mount multiple ActionCable cables?可以安装多条 ActionCable 电缆吗?
【发布时间】:2018-03-29 08:02:29
【问题描述】:

是否可以在同一个 Rails 应用程序中安装多个 ActionCable 电缆?比如这样:

#routes.rb
Rails.application.routes.draw do
  ...
  mount ActionCable.server => '/cable'
  mount ActionCable.server => '/cable2'
end

我知道我可以使用相同的电缆拥有多个频道,但我需要为我的频道使用不同的身份验证方法。据我了解,使用同一根电缆是不可能的。

感谢您的帮助。

【问题讨论】:

  • 好主意,你试过了吗?发生什么了?请这样做 - 这很可能会回答您的问题:只需尝试....
  • 为什么不将身份验证逻辑移到订阅请求及其处理程序中?这样,您只运行一个 ActionCable 服务器,每个客户端使用较少的 TCP/IP 连接,并对订阅和身份验证施加完全控制......?
  • @myst,感谢您的评论。我需要 2 种不同的身份验证方法,请在此处查看详细信息:stackoverflow.com/questions/46689243/…。我尝试了很多,但只使用一个连接没有成功。

标签: ruby-on-rails websocket ruby-on-rails-5 actioncable


【解决方案1】:

正如Using ActionCable with multiple identification methods 中所述,在同一个 Rails 应用程序中使用不同身份验证方法的一种方法是:

# 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
  ...

【讨论】:

  • 此答案不回答问题。它可能会有所帮助,但绝对不能回答这个问题。 ://
【解决方案2】:

不,它不可能开箱即用,因为您只能在 Rails 中配置一台服务器。

config/environments/development.rb 文件(或其他任何环境)中,您只有一个action_cable 配置点:

  # Mount Action Cable outside main process or domain
  config.action_cable.mount_path = nil
  config.action_cable.url = 'wss://example.com/cable'
  config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

同样在你的布局文件中你只能有一个action_cable_meta_tag

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    ...

    <%= action_cable_meta_tag %>
  </head>
  <body>
    ...
  </body>
</html>

为了拥有许多有线服务器,您必须能够像在 Hash 中那样配置其中的许多服务器:

  # Mount Action Cable outside main process or domain
  config.action_cable = [
    {
       mount_path: nil
       url: 'wss://example.com/cable'
       allowed_request_origins: [ 'http://example.com', /http:\/\/example.*/ ]
    },
    {
       mount_path: nil
       url: 'wss://example.com/cable2'
       allowed_request_origins: [ 'http://example.com', /http:\/\/example.*/ ]
    }
  ]

并且可以使用action_cable_meta_tags(注意复数版本)助手来设置它们。

但是

但是 Rails 允许您以独立模式运行您的服务器,这就是我们在我公司所做的。

所以我们使用 puma/unicorn 运行有线服务器,然后我们没有使用 action_cable_meta_tag 标签,但我们强制将 URL 指向 ActionCable.createConsumer

  const cable = ActionCable.createConsumer('wss://cable1.domain.co/cable')
  const channel = cable.subscriptions.create(...)

知道了这一点,您可以在许多主机或端口上运行许多有线服务器,然后为每个服务器创建许多 ActionCable.createConsumer 实例。

这样你就有很多有线电视服务器了。

我希望这可以帮助任何想要运行多个有线服务器的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多