加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails中,Actioncable广播无法在控制台中运行

发布时间:2020-12-17 02:09:46 所属栏目:百科 来源:网络整理
导读:我在rails 5 app上使用了actioncable.下面的代码在控制器中工作但不在控制台中. ActionCable.server.broadcast "connector_app",content: "test message" 响应: [ActionCable] Broadcasting to connector_app: {:content="test message"}= nil cable.yml de
我在rails 5 app上使用了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)

解决方法

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

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

cable.yml

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读