Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Actioncable:计数器不会增加live,即使用户已成功订阅该频道_Ruby On Rails_Redis_Actioncable_Cloud9 - Fatal编程技术网

Ruby on rails Actioncable:计数器不会增加live,即使用户已成功订阅该频道

Ruby on rails Actioncable:计数器不会增加live,即使用户已成功订阅该频道,ruby-on-rails,redis,actioncable,cloud9,Ruby On Rails,Redis,Actioncable,Cloud9,我相信我已经在cloud9上的rails应用程序中正确设置了Actioncable,但我似乎不明白,如果用户对某篇文章发表评论,为什么计数器不会增加live,消息不会自动出现在通知列表中 我能够成功订阅通知频道 计数器更新和通知只有在我重新加载页面后才会呈现,但我希望它能使用Actioncable自动更新 Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE:

我相信我已经在cloud9上的rails应用程序中正确设置了Actioncable,但我似乎不明白,如果用户对某篇文章发表评论,为什么计数器不会增加live,消息不会自动出现在通知列表中

我能够成功订阅通知频道

计数器更新和通知只有在我重新加载页面后才会呈现,但我希望它能使用Actioncable自动更新

Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
Registered connection (Z2lkOi8vd2ViLWFwcC9Vc2VyLzI)
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notifications_channel
广播作业已排队,消息称已使用正确的通知计数和id呈现部分

Notification Load (1.1ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."id" = $1 LIMIT $2  [["id", 23], ["LIMIT", 1]]
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5] Performing NotificationBroadcastJob (Job ID: 7496ec3f-891b-47e2-b7e6-37d954aae3f5) from Async(default) with arguments: 22, #<GlobalID:0x007fa20330ec40 @uri=#<URI::GID gid://web-app/Notification/23>>
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5]   Rendered notifications/_counter.html.erb (0.6ms)
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5]   Comment Load (0.4ms)  SELECT  "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT $2  [["id", 15], ["LIMIT", 1]]
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5]   Article Load (0.3ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 19], ["LIMIT", 1]]
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5]   User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5]   Rendered notifications/_notification.html.erb (7.0ms)
[ActiveJob] [NotificationBroadcastJob] [7496ec3f-891b-47e2-b7e6-37d954aae3f5] [ActionCable] Broadcasting to notification_channel: {:counter=>"<span class = \"glyphicon glyphicon-bell\"></span>\n<span id=\"notification-counter\">22</span>\n", :notification=>"  <div class = \"row\" data-notification-id= \"23\">\n    <div class= \"col-xs-10 col-md-10\">\n          <a href=\"/articles/si-ergo-lumen-quod\">\n            <li>Test1 posted a Comment on Si ergo lumen, quod </li> \n</a>    </div>\n    <div class= \"col-xs-2 col-md-2\">  \n      <a class=\"delete_notification\" data-remote=\"true\" rel=\"nofollow\" data-method=\"delete\" href=\"/notifications/23\">\n        <span class=\"glyphicon glyphicon-remove\"></span>\n</a>    </div>\n  </div>\n  <hr>\n"}
通知广播作业

class NotificationsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "notifications_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end
class NotificationBroadcastJob < ApplicationJob
  queue_as :default

  def perform(counter,notification)
    ActionCable.server.broadcast 'notification_channel',  counter: render_counter(counter), notification: render_notification(notification)
  end

  private

  def render_counter(counter)
    ApplicationController.renderer.render(partial: 'notifications/counter', locals: { counter: counter })
  end

  def render_notification(notification)
    ApplicationController.renderer.render(partial: 'notifications/notification', locals: { notification: notification })
  end  
<span id="notification-counter"><%= counter %></span>
<li>Comment posted on <%= notification.notifiable.article.title %></li> 
更新2:

<%= render "notifications/notification_center", notifications: @notifications %>
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
    <%= render 'notifications/counter', counter: notifications.count %>
</a>

<ul class="dropdown-menu" id="notificationContainer">
      <div id="notificationTitle">Notifications</div>
      <div id="notificationsBody">
        <ul id="notificationList">
          <%= render notifications %>
        </ul>
      </div>
      <div id="notificationFooter">
        <%= link_to notifications_path do %>
          Show More
        <% end %>
      </div>
</ul>
评论管理员

  def create
    @comment = @article.comments.new(comment_params)
    @comment.user = current_user
      if @comment.save
        flash[:success] = "Your comment was created successfully"
        unless @comment.article.user == current_user
          Notification.create!(recipient: @article.user, actor: current_user, action: "posted", notifiable: @comment)
        end
        redirect_to @article
      else
        flash[:error] = "Unable to submit comment."
        redirect_to @article
      end
  end

你能看到广播发送给客户吗?Chrome的开发者工具可以让你看到电缆的每一帧,这样你就可以看到你的广播是否发送到客户端。@DanielWestendorf不,广播没有发送到客户端,因为在我提交新评论后websocket消息内容没有出现。在我点击提交后,流似乎停止了。您的表单是用AJAX提交的吗?您需要阻止页面导航才能看到相同的页面更新。@DanielWestendorf刚刚发布了注释控制器。不,表单不是用Ajax提交的。保存评论后,将创建通知并重新加载页面。您的表单需要是远程表单,并使用Ajax提交,以防止频道取消订阅,从而无法获得更新。您是否能够看到广播发送到客户端?Chrome的开发者工具可以让你看到电缆的每一帧,这样你就可以看到你的广播是否发送到客户端。@DanielWestendorf不,广播没有发送到客户端,因为在我提交新评论后websocket消息内容没有出现。在我点击提交后,流似乎停止了。您的表单是用AJAX提交的吗?您需要阻止页面导航才能看到相同的页面更新。@DanielWestendorf刚刚发布了注释控制器。不,表单不是用Ajax提交的。保存评论后,将创建通知并重新加载页面。您的表单需要是远程表单,并使用Ajax提交,以防止频道取消订阅,从而永远无法获得更新。
<li>Comment posted on <%= notification.notifiable.article.title %></li> 
Started GET "/cable/" [WebSocket] for 103.252.202.198 at 2017-11-02 19:04:35 +0000
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
Registered connection (Z2lkOi8vd2ViLWFwcC9Vc2VyLzE)
NotificationsChannel is transmitting the subscription confirmation
NotificationsChannel is streaming from notifications_channel
ConversationChannel is transmitting the subscription confirmation
ConversationChannel is streaming from conversations-1
Finished "/cable/" [WebSocket] for 103.252.202.198 at 2017-11-02 19:04:46 +0000
NotificationsChannel stopped streaming from notifications_channel
ConversationChannel stopped streaming from conversations-1
Started POST "/articles/quia-receperunt-merc/comments" for 103.252.202.198 at 2017-11-02 19:04:47 +0000
Cannot render console from 103.252.202.198! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"G1ygwktPQhsScBjGyr9bCz8aZMx8wC39aVG3NKmXtYkjSsbHnbygO1cmltLi/PtH4qVdbJaYorDkoV/xFPcPRA==", "comment"=>{"content"=>"why is it not updating live"}, "commit"=>"Add Comment", "article_id"=>"quia-receperunt-merc"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Article Load (0.4ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."slug" = $1 LIMIT $2  [["slug", "quia-receperunt-merc"], ["LIMIT", 1]]
   (0.8ms)  BEGIN
  SQL (0.6ms)  INSERT INTO "comments" ("content", "article_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["content", "why is it not updating live"], ["article_id", 14], ["user_id", 1], ["created_at", "2017-11-02 19:04:47.088094"], ["updated_at", "2017-11-02 19:04:47.088094"]]
   (2.4ms)  COMMIT
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
   (0.1ms)  BEGIN
  SQL (1.1ms)  INSERT INTO "notifications" ("recipient_id", "actor_id", "action", "notifiable_id", "notifiable_type", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["recipient_id", 2], ["actor_id", 1], ["action", "posted"], ["notifiable_id", 14], ["notifiable_type", "Comment"], ["created_at", "2017-11-02 19:04:47.094991"], ["updated_at", "2017-11-02 19:04:47.094991"]]
   (1.9ms)  COMMIT
   (0.5ms)  SELECT COUNT(*) FROM "notifications"
[ActiveJob] Enqueued NotificationBroadcastJob (Job ID: ac5a005f-84d9-4ac8-be89-9f579732b167) to Async(default) with arguments: 13, #<GlobalID:0x007f10f0c6bc88 @uri=#<URI::GID gid://web-app/Notification/13>>
Redirected to https://test-site-dianaow.c9users.io/articles/quia-receperunt-merc
Completed 302 Found in 56ms (ActiveRecord: 8.7ms)
production:
  adapter: redis
  url: <%= ENV["REDISCLOUD_URL"] %>

local: &local
  adapter: redis
  url: redis://127.0.0.1:6379/1

development: *local
test: *local
  def create
    @comment = @article.comments.new(comment_params)
    @comment.user = current_user
      if @comment.save
        flash[:success] = "Your comment was created successfully"
        unless @comment.article.user == current_user
          Notification.create!(recipient: @article.user, actor: current_user, action: "posted", notifiable: @comment)
        end
        redirect_to @article
      else
        flash[:error] = "Unable to submit comment."
        redirect_to @article
      end
  end