Class: Jabber::BoshSession

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

Overview

XMPP Over BOSH class NOTE: xmpp.org/extensions/xep-0206.html

Constant Summary collapse

DEFAULTS =

Public: Default connection options

{
  host: "localhost",
  port: 5280,
  bind_uri: "/http-bind"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, bind_uri) ⇒ BoshSession

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

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

Examples

Jabber::BoshSession.new(“localhost”, 5280, “/http-bind”)

Returns Jabber::BoshSession



65
66
67
68
# File 'lib/jabber4r/bosh_session.rb', line 65

def initialize(host, port, bind_uri)
  @host, @port, @bind_uri = host, port, bind_uri
  @alive = false
end

Instance Attribute Details

#bind_uriObject (readonly)

Returns the value of attribute bind_uri.



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

def bind_uri
  @bind_uri
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#jidObject (readonly)

Returns the value of attribute jid.



25
26
27
# File 'lib/jabber4r/bosh_session.rb', line 25

def jid
  @jid
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#ridObject (readonly)

Returns the value of attribute rid.



25
26
27
# File 'lib/jabber4r/bosh_session.rb', line 25

def rid
  @rid
end

#sidObject (readonly)

Returns the value of attribute sid.



25
26
27
# File 'lib/jabber4r/bosh_session.rb', line 25

def sid
  @sid
end

#stream_idObject (readonly)

Returns the value of attribute stream_id.



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

def stream_id
  @stream_id
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)

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

Examples

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

Raises Jabber::AuthenticationError Returns Jabber::BoshSession



45
46
47
48
49
50
51
52
# File 'lib/jabber4r/bosh_session.rb', line 45

def self.bind(username, password, options = {})
  host, port, bind_uri = DEFAULTS.dup.merge!(options).values

  session = new(host, port, bind_uri)
  raise 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)


90
91
92
# File 'lib/jabber4r/bosh_session.rb', line 90

def alive?
  @alive
end

#authenticate(username, password) ⇒ Object

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

Examples

bosh = Jabber::BoshSession.new … bosh.authenticate(“strech@localhost/my-resource”, “super-secret-password”) # => true bosh.alive? # => true

Returns boolean



80
81
82
83
84
85
# File 'lib/jabber4r/bosh_session.rb', line 80

def authenticate(username, password)
  open_new_stream

  @jid = username.is_a?(JID) ? username : JID.new(username)
  @alive = (jid, password)
end

#to_jsonObject

Public: Represent BOSH-session as json object

Returns String



97
98
99
# File 'lib/jabber4r/bosh_session.rb', line 97

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