-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathpubsub.rb
More file actions
36 lines (30 loc) · 649 Bytes
/
Copy pathpubsub.rb
File metadata and controls
36 lines (30 loc) · 649 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
require 'agoo'
class Clock
def self.call(env)
unless env['rack.upgrade?'].nil?
env['rack.upgrade'] = Clock
[ 200, { }, [ ] ]
else
[ 404, { }, [ ] ]
end
end
def self.on_open(client)
client.subscribe('time')
end
Thread.new {
loop do
now = Time.now
Agoo.publish('time', "%02d:%02d:%02d" % [now.hour, now.min, now.sec])
sleep(1)
end
}
end
Agoo::Server.init(6464, '.', thread_count: 0)
Agoo::Server.handle(:GET, '/upgrade', Clock)
Agoo::Server.start()
# Quick start:
#
# ruby pubsub.rb
#
# Open either http://localhost:6464/websocket.html or
# http://localhost:6464/sse.html.