2014-01-15 46 views
1

以下是实现长轮询的代码。向控制器发送每个请求的长轮询实施

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 

的问题是所述第一请求被与所述消息

模板丢失 缺失模板你好/索引,应用程序/索引含{立即失败:区域设置=> [:EN]:格式= > [:html],:handlers => [:erb,:builder,:raw,:ruby,:jbuilder,:coffee]}。搜查:*“/家/ MYHOME的/ tmp /聊天/应用/视图”

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

回答

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 
    } 
` 
+0

返回相同的错误。没有变化 –

+0

只是好奇,你有没有尝试过在错误发生之前立即打印acArray的长度值? – rboling

+0

我放置** p ApplicationController.acArray.length **之前**下一个**在其中打印1错误之前的块 –