-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
53 lines (41 loc) · 898 Bytes
/
app.rb
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
44
45
46
47
48
49
50
51
52
53
require 'sinatra'
class StreamingEnabledMode
def should_stream?
true
end
def streamed_one_event
end
end
class Kirill < Sinatra::Base
@@streaming_mode = StreamingEnabledMode.new
def self.streaming_mode=(new_streaming_mode)
@@streaming_mode = new_streaming_mode
end
listeners = []
get '/' do
'Hi from Kirill!'
end
get '/listen' do
erb :listen
end
post '/api/note-on' do
listeners.each do |listener|
listener << "event:note-on\ndata:{\"frequency\":100}\n\n"
@@streaming_mode.streamed_one_event
end
'OK'
end
get '/api/listen' do
content_type "text/event-stream"
stream do |listener|
listeners << listener
while @@streaming_mode.should_stream? do
trap "SIGINT" do
exit 130
end
end
listeners.delete(listener)
end
end
run! if app_file == $PROGRAM_NAME
end