ActionCable 未接收数据 [英] ActionCable Not Receiving Data

查看:51
本文介绍了ActionCable 未接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ActionCable 创建了以下内容,但无法接收正在广播的任何数据.

I created the following using ActionCable but not able to receive any data that is being broadcasted.

评论频道:

class CommentsChannel < ApplicationCable::Channel
  def subscribed
    comment = Comment.find(params[:id])
    stream_for comment
  end
end

JavaScript:

var cable = Cable.createConsumer('ws://localhost:3000/cable');

var subscription = cable.subscriptions.create({
  channel: "CommentsChannel",
  id: 1
},{
  received: function(data) {
    console.log("Received data")
  }
});

它连接良好,我可以在日志中看到以下内容:

It connects fine and I can see the following in the logs:

CommentsChannel is streaming from comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x

然后我广播到那个流:

ActionCable.server.broadcast "comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x", { test: '123' }

问题是 received 函数从未被调用.我做错了什么吗?

The issue is that the received function is never called. Am I doing something wrong?

注意:我使用 actioncable npm 包从 BackboneJS 应用程序连接.

Note: I'm using the actioncable npm package to connect from a BackboneJS application.

推荐答案

config/cable.yml 中将电缆适配器从 async 更改为 redis代码> 为我修复了它.不知道为什么 async 不起作用.

Changing the cable adapter from async to redis in config/cable.yml fixed it for me. Not sure why async was not working.

这篇关于ActionCable 未接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆