Skip to content

Instantly share code, notes, and snippets.

@zotherstupidguy
Forked from kimoto/yes_web_api.rb
Created August 18, 2016 05:07
Show Gist options
  • Select an option

  • Save zotherstupidguy/c140be3f510569d39ea20ddf8dca3fe7 to your computer and use it in GitHub Desktop.

Select an option

Save zotherstupidguy/c140be3f510569d39ea20ddf8dca3fe7 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