【发布时间】:2017-06-27 10:42:43
【问题描述】:
我对 actioncable 有疑问。 我有两个通道(Notification 和 Post),但只有 Notification 流数据到接收方法而不是 Post。
PostChannel 看起来像这样 =>
class PostChannel < ApplicationCable::Channel
def subscribed
stream_from "post_channel__user_#{current_user.id}"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
PostBroadcastJob 看起来像这样 =>
class PostBroadcastJob < ApplicationJob
queue_as :default
def perform(match)
ActionCable.server.broadcast "post_channel_user_#{match.user_id}", match: render_matching(match)
end
private
def render_matching(match)
ApplicationController.renderer.render(partial: 'match/match', locals: { match: match })
end
end
我的工作名称如下所示 =>def create
if params[:remotipart_submitted]
respond_to do |f|
@post = current_user.posts.create(post_params)
@ma = Post.matching(@post.Heure,@post.Date,@post.type_activite_id).second
if @ma.nil?
puts " Aucun match"
else
match=@ma
PostBroadcastJob.perform_later(match)
end
f.html { redirect_to user_url(current_user)}
f.js
end
else
flash[:danger]= "nooooooo"
end
end
我的咖啡文件看起来像这样 => `App.post = App.cable.subscriptions.create "PostChannel", 已连接:->
当订阅准备好在服务器上使用时调用
断开连接:->
当订阅被服务器终止时调用
收到:(匹配)->
当该通道的 websocket 上有传入数据时调用
$('#notificationList').prepend "okok #{match}"`
结果 => [ActiveJob] Enqueued NotificationBroadcastJob (Job ID: 59cf14ec-4f76-4b7d-af80-b0bcf6c65986) to Async(default) with arguments: 9, #<GlobalID:0xe483670 @uri=#<URI::GID gid://see-u/Notification/89>>
Notification Load (0.0ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."id" = ? LIMIT ? [["id", 89], ["LIMIT", 1]]
Post Load (1.0ms) SELECT "posts".* FROM "posts" WHERE (Heure BETWEEN '2000-01-01 10:54:00 UTC' AND '2000-01-01 12:54:00 UTC' AND Date = '2017-06-27' AND type_activite_id = '9') ORDER BY "posts"."created_at" DESC LIMIT ? OFFSET ? [["LIMIT", 1], ["OFFSET", 1]]
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Performing NotificationBroadcastJob from Async(default) with arguments: 9, #<GlobalID:0xe461668 @uri=#<URI::GID gid://see-u/Notification/89>>
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Relationship Load (0.0ms) Select followed_id from relationships where follower_id =10
Rendered match/_match.html.erb (0.0ms)
[ActionCable] Broadcasting to post_channel_user_8: {:match=>"kklklkklkllklklklk"}
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Rendered notifications/_counter.erb (4.0ms)
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Rendered notifications/_notification.html.erb (3.0ms)
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] [ActionCable] Broadcasting to notification_channel_user_7: {:counter=>"<li class=\"zoome\">\n <img src=\"/assets/alarm-3bb640e80c366ebf278c6ed6d5cc214cefed0b62e6af95d3436dc438b5e6a250.png\" alt=\"Alarm\" />\n <span id=\"notification-counter\">9</span>\n</li>", :notification=>"<li style=\"text-decoration: none;border-bottom: 1px dotted gray;\">\n\n<div class=\"row\" style=\"margin-bottom: 0px\">\n <span class=\"col s2\" style=\"margin-top: 23px\">\n\n\n <img height=\"55\" width=\"55\" src=\"/system/users/avatars/000/000/010/thumb/boir2.jpg?1491145350\" alt=\"Boir2\" />\n\n\n </span>\n\n <span class=\"col s10\" style=\"margin-top: 15px\"> <a style=\"color: crimson;font-size: 13px\" href=\"/users/10\">Nouvelle activité proposée par Ng</a></span>\n\n\n\n</div>\n\n</li>"}
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Rendered notifications/_counter.erb (85.0ms)
Rendering posts/create.js.erb
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Rendered notifications/_notification.html.erb (0.0ms)
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] [ActionCable] Broadcasting to notification_channel_user_8: {:counter=>"<li class=\"zoome\">\n <img src=\"/assets/alarm-3bb640e80c366ebf278c6ed6d5cc214cefed0b62e6af95d3436dc438b5e6a250.png\" alt=\"Alarm\" />\n <span id=\"notification-counter\">9</span>\n</li>", :notification=>"<li style=\"text-decoration: none;border-bottom: 1px dotted gray;\">\n\n<div class=\"row\" style=\"margin-bottom: 0px\">\n <span class=\"col s2\" style=\"margin-top: 23px\">\n\n\n <img height=\"55\" width=\"55\" src=\"/system/users/avatars/000/000/010/thumb/boir2.jpg?1491145350\" alt=\"Boir2\" />\n\n\n </span>\n\n <span class=\"col s10\" style=\"margin-top: 15px\"> <a style=\"color: crimson;font-size: 13px\" href=\"/users/10\">Nouvelle activité proposée par Ng</a></span>\n\n\n\n</div>\n\n</li>"}
[ActiveJob] [NotificationBroadcastJob] [59cf14ec-4f76-4b7d-af80-b0bcf6c65986] Performed NotificationBroadcastJob from Async(default) in 1075.4ms
NotificationsChannel transmitting {"counter"=>"<li class=\"zoome\">\n <img src=\"/assets/alarm-3bb640e80c366ebf278c6ed6d5cc214cefed0b62e6af95d3436dc438b5e6a250.png\" alt=\"Alarm\" />\n <span id=\"notification-counter\">9</span>\n</li>", "notification"=>"<li style=\"text-decoration: none;border-bottom: 1px dotted gray;\">\n\n<d... (via streamed from notification_channel_user_8)
Rendered posts/_post.html.erb (19.5ms)
Rendered posts/create.js.erb (40.4ms)
Completed 200 OK in 3594ms (Views: 722.0ms | ActiveRecord: 205.7ms)
感谢大家的帮助
【问题讨论】:
标签: ruby-on-rails ruby stream actioncable