【发布时间】:2016-07-15 23:20:12
【问题描述】:
我使用 ActionCable 创建了以下内容,但无法接收正在广播的任何数据。
评论频道:
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")
}
});
连接正常,我可以在日志中看到以下内容:
CommentsChannel is streaming from comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x
然后我向该流广播:
ActionCable.server.broadcast "comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x", { test: '123' }
问题是永远不会调用received 函数。我做错了吗?
注意:我使用 actioncable npm 包从 BackboneJS 应用程序连接。
【问题讨论】:
-
在
config/cable.yml中将电缆适配器从async更改为redis修复了它。