-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamingServerConfig.ru
80 lines (56 loc) · 1.32 KB
/
StreamingServerConfig.ru
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- encoding: utf-8 -*-
# config.ru
require 'rack/stream'
require 'dotenv'
require "polyglot"
require "pp"
Dotenv.load(File.expand_path("../.env", __FILE__))
Faye::WebSocket.load_adapter('thin')
class App
include Rack::Stream::DSL
stream do
after_open do
EM.add_periodic_timer(30) {
chunk "\n"
pp "keep alive"
}
EM.add_periodic_timer(10) {
time_on_the_road = Random.rand(1..ENV['TIME_ON_THE_ROAD_VARIANCE'].to_i)
chunk "Time on the road Mills to Wilkin #{time_on_the_road} seconds\n"
pp "Time on the road Mills to Wilkin #{time_on_the_road} seconds\n"
}
end
before_close do
chunk "END!\n"
end
[200, {'Content-Type' => 'text/plain'}, []]
end
####
end
class TeamTrackFilter
include Rack::Stream::DSL
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
env['rack.stream'].instance_eval do
before_chunk do |chunks|
chunks.map { |chunk|
if chunk != "\n"
"#{parser.parse(chunk).to_json}\n" rescue "#{chunk}\n"
else
"\n"
end
}
end
end
[status, headers, response]
end
end
app = Rack::Builder.app do
use Rack::Stream
use TeamTrackFilter
run App.new
end
run app