File: client.rb

package info (click to toggle)
ruby-faye 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,792 kB
  • sloc: javascript: 14,833; ruby: 5,068; makefile: 30
file content (55 lines) | stat: -rw-r--r-- 1,456 bytes parent folder | download
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
# 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 'bundler/setup'
require 'faye'
require 'permessage_deflate'

port     = ARGV[0] || 9292
path     = ARGV[1] || 'bayeux'
scheme   = ARGV[2] == 'tls' ? 'https' : 'http'
endpoint = "#{ scheme }://user:pass@0.0.0.0:#{ port }/#{ path }"
proxy    = { :headers => { 'User-Agent' => 'Faye' }}

EM.run {
  puts "Connecting to #{ endpoint }"

  client = Faye::Client.new(endpoint, :proxy => proxy)
  client.add_websocket_extension(PermessageDeflate)

  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
}