-
Notifications
You must be signed in to change notification settings - Fork 2
/
manual_faye_message_receiver.rb
58 lines (49 loc) · 1.15 KB
/
manual_faye_message_receiver.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
54
55
56
57
58
#Requires
require 'faye'
require 'eventmachine'
# The usual defines
#FAYE_TOKEN = 'miauhash'
#FAYE_CHANNEL_PREFIX = '/messagebox/'
#FAYE_DEFAULT_CHANNEL = 'lobby'
#FAYE_SERVER_PORT = 9292
#FAYE_MOUNT_POINT = '/faye'
#FAYE_SERVER_URL = "http://localhost:#{FAYE_SERVER_PORT}#{FAYE_MOUNT_POINT}"
require File.expand_path('../config/initializers/app_config_load.rb', __FILE__)
canal = FAYE_DEFAULT_CHANNEL
canal_all = "#{FAYE_CHANNEL_PREFIX}#{FAYE_DEFAULT_CHANNEL}"
#Initialization
state = 0
puts "Initializing Faye Client ..."
begin
client = Faye::Client.new(FAYE_SERVER_URL)
puts "Faye Client started"
rescue
puts "Failed to connect to Faye server"
state += 1
end
listen_thread = nil
if state == 1
else
puts "Initializing EventMachine..."
#DO STUFF
begin
listen_thread = Thread.new do
EM.run do
puts "Listening on '#{canal_all}' ..."
client.subscribe("#{canal_all}") do |message|
puts "(#{canal})>> #{message}"
end
end
end
puts "EM started!"
rescue
puts "Event Machine dead..."
end
end
# stops
line =''
while !line.eql? "exit\n"
line = $stdin.gets
end
listen_thread.exit
puts 'Done!'