ActionController::Live Is it possible to check if connection is still alive?

后端 未结 1 550
醉梦人生
醉梦人生 2021-01-31 20:21

I\'m trying to implement text/event-stream using Rails 4\'s Live streaming. It works great and the only trouble I met is that I can\'t check if the connection is alive without s

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 20:58

    Ok, I found two options:

    1) Funny but not good (as I didn't get what sever should I use to handle 1000's of parallel connections):

    begin
      ticker = Thread.new { loop { sse.write 0; sleep 5 } }
      sender = Thread.new do
        redis.subscribe(:info, :chat) do |on|
          on.message do |event, message|
            sse.write(message, :event => event.to_s)
          end
        end
      end
      ticker.join
      sender.join
    rescue IOError
    ensure
      Thread.kill(ticker) if ticker
      Thread.kill(sender) if sender
      track.close
      sse.close
    end
    

    2) Awesome. To use Goliath server. It turned out that it can check if connection is lost without any ticker. On the way to Goliath found Cramp. It's lightweight and hopefully fast, but seems to be abandonned.

    0 讨论(0)
提交回复
热议问题