async requests using sinatra streaming API

前端 未结 1 1672
慢半拍i
慢半拍i 2020-12-21 04:22

I use async_sinatra gem to implement asynchronous routes, but I came across a post somewhere that said that Sinatra\'s streaming API can be used instead of

相关标签:
1条回答
  • 2020-12-21 04:59

    to answer my own question:

    require 'em-hiredis'
    require 'sinatra/base'
    
    class App < Sinatra::Base
      def redis
        @redis ||= EM::Hiredis.connect
      end
    
      get '/' do
        stream :keep_open do |out|
          redis.blpop('abcdef', 15).callback do |x|
            out << "x=#{x}"
            out.close
          end.errback do |e|
            out << "e=#{e}"
            out.close
          end
        end
      end
    
      run! if app_file == $0
    end
    
    0 讨论(0)
提交回复
热议问题