【问题标题】:Actioncable broadcast not working from console in Rails在 Rails 中的控制台无法使用 Actioncable 广播
【发布时间】:2017-07-22 02:44:15
【问题描述】:

我在 Rails 5 应用程序上使用了 actioncable。下面的代码在控制器中工作,但不在控制台中。

ActionCable.server.broadcast "connector_app", content: "test message"

回复:

[ActionCable] Broadcasting to connector_app: {:content=>"test message"}
=> nil

cable.yml

development:
  adapter: redis
  url: redis://localhost:6379/1

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379/1

控制器代码(正常工作):

def sample_socket_message
    ActionCable.server.broadcast "connector_app",
      content:  "test message",
    head :ok
end

已解决的问题: 忘记在 config/initializer/redis.rb 中添加以下代码

$redis = Redis.new(:host => 'localhost', :port => 6379)

【问题讨论】:

  • 我仍然有同样的问题,即使使用正确的 redis 配置,还有其他建议吗?
  • 终端可以显示哪些错误信息?
  • 我也解决了这个问题。我将电缆指向 redis 中的另一个 DB id,而不是应用程序的 config/initializers/redis.rb 中的那个

标签: ruby-on-rails actioncable


【解决方案1】:

ActionCable 在开发和测试模式下的默认行为是使用异步适配器,它只在同一进程中运行。对于进程间广播,您需要切换到 redis 适配器。

由于您在开发中使用 redis 适配器,这就是它在控制器中工作而不在控制台中工作的原因。

cable.yml

test:
  adapter: redis
  url: redis://localhost:6379/1

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    相关资源
    最近更新 更多