Created
November 20, 2012 10:06
-
-
Save kurotaky/4117088 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://blog.uu59.org/2011-10-03-sinatra-stream.html | |
require "rubygems" | |
require "sinatra" | |
set :server, :thin # 注: :keep_openが使えるのはイベントベースのサーバだけなのでthinを明示的に指定してる | |
connections = [] | |
get '/' do | |
# keep stream open | |
stream(:keep_open) { |out| connections << out } | |
end | |
post '/' do | |
# write to all open streams | |
connections.each { |out| out << params[:message] << "\n" } | |
"message sent" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment