每次请求控制器的长轮询实现

时间:2014-01-15 06:27:44

标签: ruby-on-rails ruby-on-rails-4

以下是实施长轮询的代码。

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  cattr_accessor :acArray
end

ApplicationController.acArray = []

class HelloController < ApplicationController
  def initialize
    ApplicationController.acArray << self
  end

  def index
    ApplicationController.acArray.each_with_index {|val, index|
      if index == 1 # only on second request serve the first request, until then hold the object in memory
          val.render :text => ApplicationController.acArray.length
      end
    }
  end
end

问题是第一个请求立即失败并显示消息

缺少模板 缺少模板hello / index,application / index with {:locale =&gt; [:en],:formats =&gt; [:html] ,:handlers =&gt; [:erb,:builder,:raw,:ruby,: jbuilder,:coffee]}。搜索:*“/ home / myhome / tmp / chat / app / views”

如何延迟渲染,不让rails搜索视图文件而不返回失败状态

1 个答案:

答案 0 :(得分:0)

也许这会奏效:

    until ApplicationController.acArray.length > 1 do |process|
    end
    ApplicationController.acArray.each_with_index{|val, index|
        if index == 1
            val.render :text => ApplicationController.acArray.length
        end 
    }
`