Skip to content

Instantly share code, notes, and snippets.

@danielhopkins
Created January 4, 2012 03:22
Show Gist options
  • Save danielhopkins/1558329 to your computer and use it in GitHub Desktop.
Save danielhopkins/1558329 to your computer and use it in GitHub Desktop.
Full chunked sinatra
Got this working under jruby 1.6.5 with trinidad as a webserver (webbrick doesn't seem to work)
require 'java'
require 'sinatra'
get '/stream' do
response['Content-Type'] = "application/json"
response['Transfer-Encoding'] = 'chunked'
Object.new.tap do |o|
def o.each
loop do
yield "{\"currentTime\":\"#{Time.now.strftime '%H:%M:%S'}\"}\n"
sleep 2
end
end
end
end
require 'uri'
require 'yajl/http_stream'
uri = URI.parse("http://localhost:3000/stream")
Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
puts hash.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment