Class: RubyBOSHClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_bosh.rb

Overview

Constant Summary collapse

BOSH_XMLNS =
'http://jabber.org/protocol/httpbind'
TLS_XMLNS =
'urn:ietf:params:xml:ns:xmpp-tls'
SASL_XMLNS =
'urn:ietf:params:xml:ns:xmpp-sasl'
BIND_XMLNS =
'urn:ietf:params:xml:ns:xmpp-bind'
SESSION_XMLNS =
'urn:ietf:params:xml:ns:xmpp-session'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid, pw, service_url) ⇒ RubyBOSHClient

Returns a new instance of RubyBOSHClient.



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_bosh.rb', line 17

def initialize(jid, pw, service_url) 
  @service_url = service_url
  @jid, @pw = jid, pw
  @host = jid.split("@").last
  @success = false
  @headers = {"Content-Type" => "text/xml; charset=utf-8",
              "Accept" => "text/xml"}
  connect
end

Instance Attribute Details

#jidObject

Returns the value of attribute jid.



16
17
18
# File 'lib/ruby_bosh.rb', line 16

def jid
  @jid
end

#ridObject

Returns the value of attribute rid.



16
17
18
# File 'lib/ruby_bosh.rb', line 16

def rid
  @rid
end

#sidObject

Returns the value of attribute sid.



16
17
18
# File 'lib/ruby_bosh.rb', line 16

def sid
  @sid
end

#successObject

Returns the value of attribute success.



16
17
18
# File 'lib/ruby_bosh.rb', line 16

def success
  @success
end

Instance Method Details

#connectObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_bosh.rb', line 27

def connect
  initialize_bosh_session
  if send_auth_request 
    send_restart_request
    request_resource_binding
    @success = send_session_request
  end

  if @success
    @rid+=1 #send this directly to the browser
  end
end

#construct_body(params = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_bosh.rb', line 47

def construct_body(params={}, &block)
  @rid ? @rid+=1 : @rid=rand(100000)

  builder = Builder::XmlMarkup.new
  parameters = {:rid => @rid, :xmlns => BOSH_XMLNS}.merge(params)

  if block_given?
    builder.body(parameters) {|body| yield(body)}
  else
    builder.body(parameters)
  end
end

#initialize_bosh_sessionObject



40
41
42
43
44
45
# File 'lib/ruby_bosh.rb', line 40

def initialize_bosh_session 
  response = deliver(construct_body(:wait => 5, :to => @host,
                                    :hold => 3, :window => 5,
                                    "xmpp:version" => '1.0'))
  parse(response)
end

#parse(_response) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/ruby_bosh.rb', line 103

def parse(_response)
  doc = Hpricot(_response)
  doc.search("//body").each do |body|
    @sid = body.attributes["sid"].to_s
  end
  _response
end

#request_resource_bindingObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruby_bosh.rb', line 77

def request_resource_binding
  request = construct_body(:sid => @sid) do |body|
    body.iq(:id => "bind_#{rand(100000)}", :type => "set", 
            :xmlns => "jabber:client") do |iq|
      iq.bind(:xmlns => BIND_XMLNS) do |bind|
        bind.resource('presently')
      end
    end
  end
  
  response = deliver(request)
  response.include?("<jid>") 
end

#send_auth_requestObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_bosh.rb', line 60

def send_auth_request 
  request = construct_body(:sid => @sid) do |body|
    auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}" 
    body.auth(Base64.encode64(auth_string).gsub(/\s/,''), 
                  :xmlns => SASL_XMLNS, :mechanism => 'PLAIN')
  end

  response = deliver(request)
  # TODO: make the following more robust
  response.include?("success")
end

#send_restart_requestObject



72
73
74
75
# File 'lib/ruby_bosh.rb', line 72

def send_restart_request
  request = construct_body(:sid => @sid, "xmpp:restart" => true, "xmlns:xmpp" => 'urn:xmpp:xbosh')
  deliver(request).include?("stream:features")
end

#send_session_requestObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby_bosh.rb', line 91

def send_session_request
  request = construct_body(:sid => @sid) do |body|
    body.iq(:xmlns => "jabber:client", :type => "set",
            :id => "sess_#{rand(100000)}") do |iq|
      iq.session(:xmlns => SESSION_XMLNS)
    end 
  end

  response = deliver(request)
  response.include?("body") 
end

#success?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/ruby_bosh.rb', line 111

def success?
  success == true
end