0

尝试使用 Sidekiq 向客户端发送推送事件。这是我的代码:

时钟.rb

every(3.minutes, 'update_events') { PushEventWorker.perform_async('events_updated') }

push_event_worker.rb

class PushEventWorker

  include Sidekiq::Worker
  sidekiq_options :retry => false, :timeout => 2.minutes

  def perform(job_name)
    job_name = job_name.to_sym

    if :events_updated
      Pusher['events'].trigger('events_updated', {})
  end

end
应用程序.html.erb
<script src="http://js.pusher.com/2.1/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">

  Pusher.log = function(message) {
    if (window.console && window.console.log) {
      window.console.log(message);
    }
  };

  var pusher = new Pusher('<%= ENV['pusher_key'] %>');

  var eventsChannel = pusher.subscribe('events');
  eventsChannel.bind('events_updated', function() {
    App.Pollers.Event.poll();
  });

</script>

我在日志中得到以下输出:

Pusher : Event recd : {"event":"pusher:pong","data":{}}

但是,当我打电话时PushEventWorker.new.perform('events_updated'),它是成功的。为什么这行得通,但上面的行不通?

4

1 回答 1

1

在您的 push_event_worker.rb 中,perform函数应如下所示:

def perform(job_name)
  job_name = job_name.to_sym

  if jobname == :events_updated
    Pusher['events'].trigger('events_updated', {})

end
于 2013-10-26T02:18:44.540 回答