javascript - actionCable rails 5.0.1 没有任何反应

标签 javascript ruby-on-rails ruby coffeescript

这不是我第一次在 rails 中安装 websocket,但是当我在我的 rails 控制台中输入它时,没有任何反应。那应该在 Chrome 控制台中打印“ok”吗?连接功能有效但未收到,此代码有什么问题?我用 redis 替换了 async,没有任何变化。

NotificationsChannel.broadcast_to(User.find_by(email: "test@mail.com"), title: "ok")

登录rails c

[ActionCable] Broadcasting to notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw: {:title=>"ok"}

=> 无

我的终端 rails s

[ActionCable] [8857] Finished "/cable/" [WebSocket] for ::1 at 2017-09-

13 11:38:00 +0200
[ActionCable] [8857] NotificationsChannel stopped streaming from notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw
Started GET "/cable" for ::1 at 2017-09-13 11:38:00 +0200
Started GET "/cable/" [WebSocket] for ::1 at 2017-09-13 11:38:00 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 8857], ["LIMIT", 1]]
[ActionCable] [8857] Registered connection (Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw)
[ActionCable] [8857] NotificationsChannel is transmitting the subscription confirmation
[ActionCable] [8857] NotificationsChannel is streaming from notifications:Z2lkOi8vZ2FtZS1saWJyYXJ5L1VzZXIvODg1Nw

这是我的 webSocket 安装:

channel/application_cable/connection.rb

 module ApplicationCable
   class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.id
    end

    protected

    def find_verified_user 
      if verified_user = env['warden'].user
        verified_user
      else
       reject_unauthorized_connection
      end
    end
  end
end

app/channels/notifications_channel.rb

class NotificationsChannel < ApplicationCable::Channel


  def subscribed
    stream_for current_user
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

app/assets/javascript/channels/notifications.coffee

App.cable.subscriptions.create channel: "NotificationsChannel",
  connected: ->
    console.log "connected"

  received: (data) ->
    console.log "ok"

广播时我的帧中没有任何内容。

最佳答案

我发现了问题 ==> Rails 版本。

如果您是这种情况,只需将 rails 5.0.1 更新到 5.0.2。

并将 cable.yml 中的 async 替换为 redis。

关于javascript - actionCable rails 5.0.1 没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46194913/

相关文章:

javascript - 无论如何我们可以设计 select2 的选项

javascript - 展平对象并将多个对象推送到数组

ruby-on-rails - 来自 sidekiq 的 Action Cable Broadcast 消息仅在刷新后显示,从控制台立即生效

ruby-on-rails - 像 Rails 应用程序一样的购物车

javascript - 设置输入文本的固定长度

javascript - 重制这个正则表达式

ruby-on-rails - 为什么我的 View 被标记为 XSS 漏洞?

ruby-on-rails - 结合 meta_search 与 acts_as_taggable_on

ruby-on-rails - bundle 的问题

ruby-on-rails - 所以我创建了一个 params 散列,给它赋值,试图提取一个值。