-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.rb
More file actions
33 lines (28 loc) · 922 Bytes
/
Copy pathapplication.rb
File metadata and controls
33 lines (28 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# coding: utf-8
class Application < Sinatra::Application
set server: 'thin'
set publisher: Publisher.new
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
settings.publisher.subscribe(params[:channel].to_s, params[:event].to_s, out)
out.callback do
settings.publisher.unsubscribe(params[:channel].to_s, params[:event].to_s, out)
end
out.errback do
settings.publisher.unsubscribe(params[:channel].to_s, params[:event].to_s, out)
end
end
end
post '/publish' do
logger.info "publishing to #{params[:channel]} - #{params[:event]}"
settings.publisher.broadcast(params[:channel].to_s, params[:event].to_s, params[:data].to_s)
204 # response without entity body
end
#render page that listens to testchannel, testevent
get '/test' do
erb :test
end
get '/connections' do
settings.publisher.to_s
end
end