-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.rb
More file actions
43 lines (33 loc) · 819 Bytes
/
Copy pathstream.rb
File metadata and controls
43 lines (33 loc) · 819 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
33
34
35
36
37
38
39
40
41
42
43
require 'json'
require 'sinatra'
set :public_folder, Proc.new { File.join(root, "public") }
set server: 'thin'
get '/' do
erb :index
end
get '/admin' do
erb :admin
end
def timestamp
Time.now.strftime("%H:%M:%S")
end
connections = []
notifications = []
get '/connect', provides: 'text/event-stream' do
stream :keep_open do |out|
connections << out
#out.callback on stream close evt.
out.callback {
#delete the connection
connections.delete(out)
}
end
end
post '/push' do
puts params
#Add the timestamp to the notification
notification = params.merge( {'timestamp' => timestamp}).to_json
notifications << notification
notifications.shift if notifications.length > 10
connections.each { |out| out << "data: #{notification}\n\n"}
end