【问题标题】:long poll implementation on every request to the controller对控制器的每个请求的长轮询实现
【发布时间】:2014-02-03 12:16:50
【问题描述】:

下面是实现长轮询的代码。

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=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :咖啡]}。搜索:*“/home/myhome/tmp/chat/app/views”

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

【问题讨论】:

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


    【解决方案1】:

    也许这会起作用:

        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 放在 next 之前的 else 块中,该块在错误前打印 1
    • 这会阻止服务器。也直到不占用进程 arg。
    猜你喜欢
    • 2013-06-12
    • 1970-01-01
    • 2010-10-27
    • 2012-03-27
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多