我希望有人有时间帮我处理我的代码。我一直坚持使用Rails 5使用Active Job in Action Cable一个多月了,似乎没有人在我的代码中发现我的课程有什么问题。
我正在处理的应用程序预计会显示2条Flash消息 1.感谢您的评论(来自AJAX) 2.发布了一条新评论(来自Javascript频道代码)
以及更多来自(来自Javascript频道代码) 3.添加新评论 4.刷新Raty评级 5.淡出flash消息
但是,动作电缆仅传输2,3,4项新评论。重新加载页面后,操作电缆将停止流式传输。
我可以在此处找到我的远程仓库 - Sample Application
示例应用可在此处找到:Demo Application
我的行动电缆的起始码是:
config/cable/yml
development:
adapter: async
test:
adapter: async
production:
adapter: postgresql
url: postgres://ybywuliqfewhzz:R4PgzgJ9IdaJ7_pdIlHkXaFkS2@ec2-54-235-221-102.compute-1.amazonaws.com:5432/dfuvk0kbffhdq5
服务器站点电缆代码app / assets / javascripts / channels / product.js
App.product = App.cable.subscriptions.create({channel:"ProductChannel"}, {
connected: function() {
// Called when the subscription is ready for use on the server
},
disconnected: function() {
// Called when the subscription has been terminated by the server
},
received: function(data) {
// Called when there's incoming data on the websocket for this channel
console.log("Hello");
$('#cable-flash').show();
$('.product-reviews').prepend(data.comment);
refreshRating();
$('.alert').delay(2000).fadeOut(3000);
},
listen_to_comments: function() {
return this.perform('listen', {
product_id: $("[data-product-id]").data("product-id")
});
}
});
$(document).on('turbolinks:load', function() {
App.product.listen_to_comments();
});
应用程序/信道/ product_channel.rb
class ProductChannel < ApplicationCable::Channel
def subscribed
# stream_from "product_channel"
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
def listen(data)
stop_all_streams
stream_for data["product_id"]
end
end
应用程序/信道/ application_cable / channel.rb
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
应用程序/信道/ application_cable / connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
def disconnect
end
protected
def find_verified_user
if verified_user = request.env['warden'].user
verified_user
else
reject_unauthorized_connection
end
end
end
end
config / environments / production.rb摘录
config.action_cable.url = "wss://cfbikeberlinapp.herokuapp.com/cable"
config.action_cable.allowed_request_origins = ['https://cfbikeberlinapp.herokuapp.com','https://cfbikeberlinapp.herokuapp.com']
end
代码在开发中完美运行但在移动和桌面的heroku中失败,它仅适用于一个注释,在该操作电缆停止传输作业之后。
这是我在重新加载页面后从heroku收到的日志的摘录,显示产品页面已停止流式传输。
016-09-14T11:53:02.035002+00:00 heroku[router]: at=info method=GET path="/products/4" host=cfbikeberlinapp.herokuapp.com request_id=ad8f16fc-ac0a-4fc6-bbaf-5182127bd89d fwd="115.64.68.87" dyno=web.1 connect=0ms service=75ms status=200 bytes=8698
2016-09-14T11:53:02.420634+00:00 heroku[router]: at=info method=GET path="/assets/application-93d18917a3b22de4e50a8d8e5993dc92db143d1f1bc5b1914661670b138d5995.css" host=cfbikeberlinapp.herokuapp.com request_id=9012c627-feb5-4b38-8fa6-3b20cbf1ed20 fwd="115.64.68.87" dyno=web.1 connect=0ms service=1ms status=304 bytes=48
2016-09-14T11:53:02.303375+00:00 app[web.1]: I, [2016-09-14T11:53:02.303306 #3] INFO -- : Finished "/cable/" [WebSocket] for 115.64.68.87 at 2016-09-14 11:53:02 +0000
2016-09-14T11:53:02.303485+00:00 app[web.1]: I, [2016-09-14T11:53:02.303443 #3] INFO -- : ProductChannel stopped streaming from product:4
2016-09-14T11:53:02.693961+00:00 heroku[router]: at=info method=GET path="/assets/application-c4e466ef03b0da2f06fb6383f1a946665201a62d89bc96973d1a3b473db1f32b.js" host=cfbikeberlinapp.herokuapp.com request_id=bfc119a2-f74f-45e8-a6c4-eeb915d54807 fwd="115.64.68.87" dyno=web.1 connect=0ms service=1ms status=304 bytes=48
2016-09-14T11:53:03.527479+00:00 heroku[router]: at=info method=GET path="/assets/star-off.png" host=cfbikeberlinapp.herokuapp.com request_id=4285511d-6809-4558-b57a-9a0ae7fb8751 fwd="115.64.68.87" dyno=web.1 connect=0ms service=4ms status=304 bytes=258