Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created June 1, 2012 08:03
Show Gist options
  • Save kimoto/2850119 to your computer and use it in GitHub Desktop.
Save kimoto/2850119 to your computer and use it in GitHub Desktop.
yes WEB API (Ruby + Sinatra/Streaming)
# coding: utf-8
require 'sinatra'
require 'sinatra/streaming'
set server: 'thin', connections: []
th = Thread.new{
while true
puts "currently connection: #{settings.connections.size}"
settings.connections.each{ |e|
e << "yes\n"
}
sleep 1
end
}
get '/' do
stream :keep_open do |out|
out.callback{ settings.connections.delete(out) }
out.errback{ settings.connections.delete(out) }
settings.connections << out
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment