Skip to content

Instantly share code, notes, and snippets.

@kurotaky
Created November 20, 2012 10:06
Show Gist options
  • Save kurotaky/4117088 to your computer and use it in GitHub Desktop.
Save kurotaky/4117088 to your computer and use it in GitHub Desktop.
# 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