Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4cbec82
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source :rubygems | ||
|
||
gem 'thin' | ||
gem 'sinatra' | ||
gem 'sinatra-contrib' | ||
gem 'haml' | ||
gem 'amqp' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
amq-client (0.9.2) | ||
amq-protocol (>= 0.9.0) | ||
eventmachine | ||
amq-protocol (0.9.0) | ||
amqp (0.9.4) | ||
amq-client (~> 0.9.2) | ||
amq-protocol (>= 0.9.0) | ||
eventmachine | ||
backports (2.3.0) | ||
daemons (1.1.8) | ||
eventmachine (0.12.10) | ||
haml (3.1.4) | ||
rack (1.4.1) | ||
rack-protection (1.2.0) | ||
rack | ||
rack-test (0.6.1) | ||
rack (>= 1.0) | ||
sinatra (1.3.2) | ||
rack (~> 1.3, >= 1.3.6) | ||
rack-protection (~> 1.2) | ||
tilt (~> 1.3, >= 1.3.3) | ||
sinatra-contrib (1.3.1) | ||
backports (>= 2.0) | ||
eventmachine | ||
rack-protection | ||
rack-test | ||
sinatra (~> 1.3.0) | ||
tilt (~> 1.3) | ||
thin (1.3.1) | ||
daemons (>= 1.0.9) | ||
eventmachine (>= 0.12.6) | ||
rack (>= 1.0.0) | ||
tilt (1.3.3) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
amqp | ||
haml | ||
sinatra | ||
sinatra-contrib | ||
thin |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: bundle exec ruby app.rb -p $PORT -e production |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'sinatra' | ||
require 'sinatra/streaming' | ||
require 'haml' | ||
require 'amqp' | ||
|
||
configure do | ||
EM.next_tick do | ||
AMQP.connect ENV['CLOUDAMQP_URL'] || 'amqp://guest:guest@localhost' | ||
end | ||
end | ||
|
||
get '/' do | ||
haml :index | ||
end | ||
|
||
post '/publish' do | ||
AMQP::Channel.new do |channel| | ||
channel.fanout("f1").publish "Hello, world!" | ||
end | ||
204 | ||
end | ||
|
||
get '/stream', provides: 'text/event-stream' do | ||
stream :keep_open do |out| | ||
AMQP::Channel.new do |channel| | ||
channel.queue do |queue| | ||
queue.bind(channel.fanout("f1")).subscribe do |payload| | ||
out << "data: #{payload}\n\n" | ||
end | ||
end | ||
timer = EM.add_periodic_timer(20) { out << ":\n" } | ||
out.callback do | ||
p 'closing' | ||
timer.cancel | ||
channel.close | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
!!! | ||
%html | ||
%head | ||
%script{src: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', type: 'text/javascript'} | ||
%body | ||
%input#publish{type: 'button', value: 'Publish'} | ||
%ul#messages | ||
:javascript | ||
var es = new EventSource('/stream'); | ||
es.onmessage = function(e) { | ||
$('#messages').append("<li>" + e.data + "</li>") | ||
} | ||
$("#publish").click(function () { | ||
$.post("/publish") | ||
}) | ||
|