我正在使用ActionCable和Rails 5.1 . 我无法让 Channels 成功地向客户端广播消息,尽管客户端可以很好地将消息发送到 Channels . 我设置了我的 Channels 文件app / channels / work_channel.rb,
class WorkChannel < ApplicationCable::Channel
# Data is received as a hash when sent by the client as JSON
def receive(data)
...
end
def subscribed
stream_from 'work'
end
...
end
我使用以下内容向我的订阅者发送消息...
ActionCable.server.broadcast 'work', message: msg
在客户端,我有这个
App.cable = ActionCable.createConsumer();
...
App.work = App.cable.subscriptions.create(
'WorkChannel',
{
received: function(data) {
console.log("received data from channel:" + data);
this._onMessage( data );
}
});
但是,在调用上面的“ActionCable.server.broadcast'work',message:msg”代码时,永远不会调用“received”函数 . 我在这里想念的是什么?我还是新手,所以我觉得我还有一些事要做 .