gpt4 book ai didi

ruby-on-rails - Actioncable 广播在 Rails 控制台中不起作用

转载 作者:行者123 更新时间:2023-12-04 15:08:07 25 4
gpt4 key购买 nike

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

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

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

电缆.yml
development:
adapter: redis
url: redis://localhost:6379/1

test:
adapter: async

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

Controller 代码(工作正常):
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)

最佳答案

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

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

电缆.yml

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

关于ruby-on-rails - Actioncable 广播在 Rails 控制台中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42554323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com