Created
December 16, 2014 16:14
-
-
Save rye/09ca51abf3f177a74f98 to your computer and use it in GitHub Desktop.
Messing around with HTTP Streaming responses.
This file contains 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
require "date" | |
require "json" | |
require "sinatra" | |
require "time" | |
module StreamMessageFormatter | |
def self.format(object) | |
return JSON.generate(object) << "\r\n" | |
end | |
end | |
get '/stream' do | |
headers "Access-Control-Allow-Origin" => "*" | |
stream do |out| | |
while true do | |
out << StreamMessageFormatter.format({time:Time.now.utc.iso8601,message:"heartbeat, baby!"}) | |
sleep 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment