-
Notifications
You must be signed in to change notification settings - Fork 17
/
faye.ru
83 lines (62 loc) · 1.72 KB
/
faye.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
81
82
83
# Namespaces in use
#
# * /dj
# * /monitoring
# * /event/talk
# * /notification
# * /stat/t1051-u1
# * /stat
# * /t1051/public (TODO rename to /live/down/t1051/public)
# * /t1051/u1 (TODO rename to /live/down/t1051/u1)
# * /live/up/t1051/u1 (will be squashed by FayeSquasher)
# => /live/up { channel: '/live/up/t1051/u1' }
#
# Run this file with
#
# rackup faye.ru -E production
#
require 'yaml'
require 'term/ansicolor'
require 'faye'
require 'faye/authentication'
require File.expand_path('../lib/faye_squasher', __FILE__)
require File.expand_path('../lib/faye_sifter', __FILE__)
class String
include Term::ANSIColor
end
# INSTANCIATE
Faye::WebSocket.load_adapter('thin')
faye = Faye::RackAdapter.new(mount: '/faye', timeout: 15)
# for debugging, output goes to log/thin.log
# Faye.logger = Logger.new(STDOUT)
# AUTHENTICATION
# TODO figure out how to use Settings here
file = File.expand_path('../config/settings.local.yml', __FILE__)
config = YAML.load(File.read(file))
secret = config['faye']['secret_token']
faye.add_extension Faye::Authentication::ServerExtension.new(secret)
# CUSTOM EXTENSIONS
rules = {
'/live/up' => %r{^/live/up/t\d+/u\d+$},
}
faye.add_extension FayeSquasher.new(rules)
faye.add_extension FayeSifter.new
# OUTPUT
env = ENV['RAILS_ENV'] || 'development'
if env == 'development'
puts "We're in dev mode, showing logs...".cyan
# black, red, green, yellow, blue, magenta, cyan, white
faye.on(:publish) do |client_id, channel, data|
puts ["publish".green,
channel.yellow,
client_id,
data.to_yaml] * ' '
end
faye.on(:subscribe) do |client_id, channel|
puts ["subscribe".red,
channel.cyan,
client_id]
end
end
# RUN
run faye