Ruby on rails 对控制器的每个请求执行长轮询
下面是实现长轮询的代码Ruby on rails 对控制器的每个请求执行长轮询,ruby-on-rails,ruby-on-rails-4,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 Appl
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
class ApplicationController
问题是第一个请求立即失败,并显示消息
缺少模板
缺少模板hello/index,带有{:locale=>[:en],:formats=>[:html],:handlers=>[:erb,:builder,:raw,:ruby,:jbuilder,:coffee]}的应用程序/索引。在以下位置搜索:“//主页/myhome/tmp/chat/app/views”
如何延迟渲染,不让rails搜索视图文件,也不返回失败状态也许这样可以:
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
}
`
好奇的是,您是否尝试在错误发生之前立即打印acArray的长度值?我将p ApplicationController.acArray.length before next放在else块中,该块在错误之前打印1。这将阻止服务器。也直到不采取过程参数。