forked from HackerSir/sheep-wall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
executable file
·74 lines (59 loc) · 1.5 KB
/
config.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
#!/usr/bin/env ruby
$:.push File.expand_path "../lib", File.dirname(__FILE__)
require "rubygems"
require "bundler/setup"
Bundler.setup(:default)
require "eventmachine"
require "faye"
require "permessage_deflate"
require "redis"
require "json"
require "pry"
class LimitPublish
def incoming(mesg,cb)
puts "\e[1;32m#{mesg.inspect}\e[m"
puts "\e[1;33m#{cb[mesg].inspect}\e[m"
end
end
def new_redis
if ENV.key?('REDIS_HOST')
redis = Redis.new host: ENV['REDIS_HOST'], port: ENV['REDIS_PORT']
elsif ENV.key? 'REDIS_URL'
redis = Redis.new url: ENV['REDIS_URL']
else
redis = Redis.new
end
redis
end
ws = Faye::RackAdapter.new mount: '/faye', ping: 5
ws.add_websocket_extension(PermessageDeflate)
ws.add_extension(LimitPublish.new)
ws.on(:subscribe) do |client_id, channel|
puts "[ SUBSCRIBE] #{client_id[0,5]} -> #{channel}"
end
ws.on(:unsubscribe) do |client_id, channel|
puts "[UNSUBSCRIBE] #{client_id[0,5]} -> #{channel}"
end
ws.on(:publish) do |client_id, channel, data|
puts "[ PUBLISH] #{client_id[0,5]} -> #{channel}: #{data}"
end
ws.on(:disconnect) do |client_id|
puts "[ DISCONNECT] #{client_id[0,5]}"
end
def ws.log mesg
puts mesg
end
Thread.new do
EM.run
end
Thread.new do
listener, fetcher = new_redis(), new_redis()
listener.subscribe("new-sheep") do |ev|
ev.message do |channel, mesg|
p mesg
ws.get_client.publish "/update", fetcher.hgetall(mesg).to_json
end
end
end
use Rack::Static, :urls => {"/" => 'index.html'}, :root => 'public'
run ws