【问题标题】:Trying to have my controller continuously fire every 60 seconds. (rails)试图让我的控制器每 60 秒连续触发一次。 (导轨)
【发布时间】:2021-03-26 15:45:54
【问题描述】:

我正在构建一个代码应用程序,它会根据每 60 秒刷新一次的数据提醒用户。我不希望来自客户端的新数据调用,但不知道如何让控制器不断进行其 API 调用并推送到客户端。

 def search
    @price = find_price()
    render json: @price
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    # def setprice
    #   @price = Price.find(params[:id])
    # end

    def request_api()
      response = Excon.get(
        'example.api'
      )

      return nil if response.status != 200
    data = JSON.parse(response.body)
    data['PriceRange']['4']
    end

    def find_price()
      request_api()
    end

    # Only allow a trusted parameter "white list" through.
    def price_params
      params.require(:price).permit(:price)
    end
end

【问题讨论】:

    标签: ruby-on-rails server-sent-events actioncable


    【解决方案1】:

    简短的回答是你不能通过控制器来完成,因为一旦控制器处理了客户端的请求,它就会“忘记”客户端。你想要的是某种ActionCable 解决方案。 ActionCable 是 Rails 设置 websocket 的方式;如果您有 websocket 连接,服务器可以将更新推送到与其连接的每个客户端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 2021-03-01
      • 2011-03-09
      相关资源
      最近更新 更多