Rails ActionCable channel 不传输消息

pain · 2015年08月03日 · 最后由 pain 回复于 2015年08月13日 · 2437 次阅读

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 的消息。求助!

`App.cable.subscriptions.create 'NotifictionsChannel' 这里是不是有个错别字

#2 楼 @zgm 是错别字,谢谢指出!不过这个不是问题的根源啊,创建和调用是一致的。

#2 楼 @zgm 就是错别字的原因!去撞墙了!

问题解决了。是我没有改 cableServer 的地址:在创建客户端接收 app 的地方把 puma server 的地址改为生产服务器的地址即可。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号