Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Last active June 30, 2017 17:28
Show Gist options
  • Save a2ikm/d4c20ec5e186564858ad2ad493ed74e5 to your computer and use it in GitHub Desktop.
Save a2ikm/d4c20ec5e186564858ad2ad493ed74e5 to your computer and use it in GitHub Desktop.
require "json"
require "rack/cors"
class Page
def each
loop do
count = [1, 2, 3].sample
words = %w(one two thee four five six seven).sample(count)
json = words.map { |word|
{ word: word }.to_json
}.join("\n\n")
yield json
sleep 1
end
end
end
use Rack::Chunked
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post], :expose => ["Transfer-Encoding"]
end
end
app = ->(env) {
[
200,
{ "Content-Type" => "text/plain" },
Page.new,
]
}
run app
# window 1
$ gem install rack-cors
$ rackup -s puma
# window 2
$ ruby -run -e httpd . -p 8000
# window 3
$ open http://localhost:8000
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>index</title>
<script src="https://raw.githubusercontent.com/eBay/jsonpipe/master/jsonpipe.js"></script>
<script>
jsonpipe.flow('http://localhost:9292', {
"success": function(data) {
console.log(data);
},
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment