channel.rb 文件:
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
notifications_channel.rb 文件:
class NotifictionsChannel < ApplicationCable::Channel
def subscribed
stream_from 'notifications'
end
end
发布广播的代码:
def create
ActionCable.server.broadcast 'notifications',
message: params[:notification][:body]
head :ok
end
订阅的代码:
App.notifications = App.cable.subscriptions.create 'NotifictionsChannel',
received: (data) ->
alert(data.message)
$('#notifications').append @renderNotification(data)
renderNotification: (data) ->
"<p>#{data.message}</p>"
我可以确定我的其他配置是正确的,因为同样地配置在另一个独立项目里是 ok 的。在这里后台提示: [ActionCable] Broadcasting to notifications: {:message=>"12"} 但似乎订阅这里一直收不到 notification channel 的消息。求助!