/
client.rb
51 lines (43 loc) · 1.28 KB
/
client.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
# This script demonstrates a logger for the chat app. First, start the chat
# server in one terminal then run this in another:
#
# $ ruby examples/ruby/server.rb
# $ ruby examples/ruby/client.rb
#
# The client connects to the chat server and logs all messages sent by all
# connected users.
require 'rubygems'
require File.expand_path('../../../lib/faye', __FILE__)
port = ARGV[0] || 9292
path = ARGV[1] || 'bayeux'
scheme = ARGV[2] == 'ssl' ? 'https' : 'http'
endpoint = "#{scheme}://localhost:#{port}/#{path}"
EM.run {
puts "Connecting to #{endpoint}"
client = Faye::Client.new(endpoint)
subscription = client.subscribe '/chat/*' do |message|
user = message['user']
publication = client.publish("/members/#{ user }", {
"user" => "ruby-logger",
"message" => "Got your message, #{ user }!"
})
publication.callback do
puts "[PUBLISH SUCCEEDED]"
end
publication.errback do |error|
puts "[PUBLISH FAILED] #{error.inspect}"
end
end
subscription.callback do
puts "[SUBSCRIBE SUCCEEDED]"
end
subscription.errback do |error|
puts "[SUBSCRIBE FAILED] #{error.inspect}"
end
client.bind 'transport:down' do
puts "[CONNECTION DOWN]"
end
client.bind 'transport:up' do
puts "[CONNECTION UP]"
end
}