我遵循了这篇教程创建了一个Action Cable广播,但它并没有像预期的那样正常工作。频道流和web应用程序成功订阅,但从Sidekiq后台作业广播的消息只有在刷新页面后才显示。在控制台上使用相同的命令会立即更新页面。
当在Chrome的开发者模式中查看帧时,无法看到来自后台作业的广播消息,但可以立即看到由控制台发送的消息。但是,我可以确认Sidekiq后台作业正在某个地方广播这些消息,因为它们在刷新后确实会显示出来;但是,我不知道它们被排队到哪里了。
是否需要进行其他配置更改以防止后台作业的消息被排队到某个地方?我的代码中是否存在任何拼写错误或错误?
Action Cable广播消息:
当在Chrome的开发者模式中查看帧时,无法看到来自后台作业的广播消息,但可以立即看到由控制台发送的消息。但是,我可以确认Sidekiq后台作业正在某个地方广播这些消息,因为它们在刷新后确实会显示出来;但是,我不知道它们被排队到哪里了。
是否需要进行其他配置更改以防止后台作业的消息被排队到某个地方?我的代码中是否存在任何拼写错误或错误?
Action Cable广播消息:
ActionCable.server.broadcast "worker_channel", {html:
"<div class='alert alert-success alert-block text-center'>
Market data retrieval complete.
</div>"
}
smart_worker.rb: -- 这个是在控制器的操作中作为perform_async调用的
class SmartWorker
include Sidekiq::Worker
include ApplicationHelper
sidekiq_options retry: false
def perform
ActionCable.server.broadcast "worker_channel", {html:
"<div class='alert alert-success alert-block text-center'>
Market data retrieval complete.
</div>"
}
end
connection.rb:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = current_user #find_verified_user ignored until method implemented correctly and does not always return unauthorized
end
private
def find_verified_user
if current_user = User.find_by(id: cookies.signed[:user_id])
current_user
else
reject_unauthorized_connection
end
end
end
end
worker_channel:
class WorkerChannel < ApplicationCable::Channel
def subscribed
stream_from "worker_channel"
end
def unsubscribed
end
end
worker.js:
App.notifications = App.cable.subscriptions.create('WorkerChannel', {
connected: function() {
console.log('message connected');
},
disconnected: function() {},
received: function(data) {
console.log('message recieved');
$('#notifications').html(data.html);
}
});
cable.yml
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: async
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: smarthost_production
也将其添加到视图中,但这并没有产生影响。