Class: Jabber::Bosh::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/bosh/session.rb

Overview

This class provide XMPP Over BOSH xmpp.org/extensions/xep-0206.html

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, port, bind_uri, options = {}) ⇒ Session

Public: Create new BOSH-session (not binded to http-bind service)

domain - String the jabber server domain port - [String|Fixnum] the port of http-bind endpoint of jabber server bind_uri - String the http-bind uri

Returns Jabber::BoshSession



64
65
66
67
68
69
# File 'lib/jabber4r/bosh/session.rb', line 64

def initialize(domain, port, bind_uri, options = {})
  @domain, @port, @bind_uri = domain, port, bind_uri
  @use_sasl = options.fetch(:use_sasl, Jabber::Bosh::DEFAULTS[:use_sasl])

  @alive = false
end

Instance Attribute Details

#bind_uriObject (readonly)

Public: Bosh service http-bind uri



28
29
30
# File 'lib/jabber4r/bosh/session.rb', line 28

def bind_uri
  @bind_uri
end

#domainObject (readonly)

Public: Bosh service domain FIXME : Replace to host



24
25
26
# File 'lib/jabber4r/bosh/session.rb', line 24

def domain
  @domain
end

#jidObject (readonly)

Public: Jabber user login



16
17
18
# File 'lib/jabber4r/bosh/session.rb', line 16

def jid
  @jid
end

#portObject (readonly)

Public: Bosh service port



26
27
28
# File 'lib/jabber4r/bosh/session.rb', line 26

def port
  @port
end

#ridObject

Public: Http request identifier



18
19
20
# File 'lib/jabber4r/bosh/session.rb', line 18

def rid
  @rid
end

#sidObject

Public: Session identifier



20
21
22
# File 'lib/jabber4r/bosh/session.rb', line 20

def sid
  @sid
end

Class Method Details

.bind(username, password, options = {}) ⇒ Object

Public: Create new BOSH-session and bind it to jabber http-bind service

username - String the login of jabber server user password - String the password of jabber server user options - Hash the options for jabber http-bind service (default: Empty hash)

:domain   - String the jabber server domain indentificator
:port     - [String|Fixnum] the port of http-bind endpoint of jabber server
:bind_uri - String the http-bind uri
:use_sasl - boolean the flag defining authentication method

Examples

Jabber::Bosh::Session.bind(“strech@localhost/res-1”, “secret-pass”) Jabber::Bosh::Session.bind(“strech@localhost/res-1”, “secret-pass”, domain: “localhost”)

Raises Jabber::AuthenticationError Returns Jabber::Bosh::Session TODO : Change arguments passing into initialize method



48
49
50
51
52
53
54
55
# File 'lib/jabber4r/bosh/session.rb', line 48

def self.bind(username, password, options = {})
  domain, port, bind_uri, use_sasl = Jabber::Bosh::DEFAULTS.dup.merge!(options).values

  session = new(domain, port, bind_uri, use_sasl: use_sasl)
  raise Jabber::AuthenticationError, "Failed to login" unless session.authenticate(username, password)

  session
end

Instance Method Details

#alive?Boolean

Public: Is BOSH-session active? (no polling consider)

Returns boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/jabber4r/bosh/session.rb', line 86

def alive?
  @alive
end

#authenticate(username, password) ⇒ Object

Public: Authenticate user in jabber server by his username and password NOTE: This authentication is SASL xmpp.org/rfcs/rfc3920.html#sasl or Non-SASL xmpp.org/extensions/xep-0078.html

Returns boolean



76
77
78
79
80
81
# File 'lib/jabber4r/bosh/session.rb', line 76

def authenticate(username, password)
  @jid = username.is_a?(Jabber::JID) ? username : Jabber::JID.new(username)

  authentication = authentication_technology.new(self)
  @alive = authentication.authenticate(jid, password)
end

#generate_next_ridObject

Internal: Generate next request id for http post request

FIXME : Will be replaced to Generator class

Returns Fixnum



134
135
136
137
# File 'lib/jabber4r/bosh/session.rb', line 134

def generate_next_rid
  @rid ||= rand(1_000_000_000)
  @rid += 1
end

#post(body) ⇒ Object

Internal: Send HTTP-post request on HTTP-bind uri

body - String data, which will be sended

Raises Net::HTTPBadResponse Returns Net:HttpResponse



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jabber4r/bosh/session.rb', line 110

def post(body)
  request = Net::HTTP::Post.new(bind_uri)
  request.body = body
  request.content_length = request.body.size
  request["Content-Type"] = "text/xml; charset=utf-8"

  Jabber.debug("Sending POST request - #{body.strip}")

  response = Net::HTTP.new(domain, port).start { |http| http.request(request) }

  Jabber.debug("Receiving POST response - #{response.code}: #{response.body.inspect}")

  unless response.is_a?(Net::HTTPSuccess)
    raise Net::HTTPBadResponse, "Net::HTTPSuccess expected, but #{response.class} was received"
  end

  response
end

#sasl?Boolean

Public: Is BOSH-session uses sasl authentication

Returns boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/jabber4r/bosh/session.rb', line 93

def sasl?
  @use_sasl
end

#to_jsonObject

Public: Represent BOSH-session as json object

Returns String



100
101
102
# File 'lib/jabber4r/bosh/session.rb', line 100

def to_json
  {jid: jid.to_s, rid: rid, sid: sid}.to_json
end