Class: Jabber::HTTPBinding::Client

Class | Jabber::HTTPBinding::Client
In: | lib/xmpp4r/httpbinding/client.rb
Parent: | Jabber::Client

This class implements an alternative Client using HTTP Binding (JEP0124).

This class is designed to be a drop-in replacement for Jabber::Client, except for the Jabber::HTTP::Client#connect method which takes an URI as argument.

HTTP requests are buffered to not exceed the negotiated ‘polling’ and ‘requests’ parameters.

Stanzas in HTTP resonses may be delayed to arrive in the order defined by ‘rid’ parameters.

# Debugging

Turning Jabber::debug to true will make debug output not only spit out stanzas but HTTP request/response bodies, too.

### Methods

close connect ensure_one_pending_request new send

### Attributes

http_content_type | [RW] | Content-Type to be used for communication (you can set this to "text/html")
http_hold | [RW] | The server may hold this amount of stanzas to reduce number of HTTP requests
http_wait | [RW] | The server should wait this value seconds if there is no stanza to be received

### Public Class methods

new(jid)

Initialize

jid: | [JID or String]

[Source]

        # File lib/xmpp4r/httpbinding/client.rb, line 45
    45:       def initialize(jid)
    46:         super
    47:
    48:         @lock = Mutex.new
    49:         @pending_requests = 0
    50:         @last_send = Time.at(0)
    51:         @send_buffer = ''
    52:
    53:         @http_wait = 20
    54:         @http_hold = 1
    55:         @http_content_type = 'text/xml; charset=utf-8'
    56:       end

### Public Instance methods

close()

Close the session by sending <presence type=‘unavailable’/>

[Source]

         # File lib/xmpp4r/httpbinding/client.rb, line 139
    139:       def close
    140:         @status = DISCONNECTED
    141:         send(Jabber::Presence.new.set_type(:unavailable))
    142:       end

connect(uri, host=nil, port=5222)

Set up the stream using uri as the HTTP Binding URI

You may optionally pass host and port parameters to make use of the JEP0124 ‘route’ feature.

uri: | [URI::Generic or String]
host: | [String] Optional host to route to
port: | [Fixnum] Port for route feature

[Source]

         # File lib/xmpp4r/httpbinding/client.rb, line 67
     67:       def connect(uri, host=nil, port=5222)
     68:         uri = URI::parse(uri) unless uri.kind_of? URI::Generic
     69:         @uri = uri
     70:
     71:         @allow_tls = false  # Shall be done at HTTP level
     72:         @stream_mechanisms = []
     73:         @stream_features = {}
     74:         @http_rid = IdGenerator.generate_id.to_i
     75:         @pending_rid = @http_rid
     76:         @pending_rid_lock = Mutex.new
     77:
     78:         req_body = REXML::Element.new('body')
     79:         req_body.attributes['rid'] = @http_rid
     80:         req_body.attributes['content'] = @http_content_type
     81:         req_body.attributes['hold'] = @http_hold.to_s
     82:         req_body.attributes['wait'] = @http_wait.to_s
     83:         req_body.attributes['to'] = @jid.domain
     84:         if host
     85:           req_body.attributes['route'] = 'xmpp:#{host}:#{port}'
     86:         end
     87:         req_body.attributes['secure'] = 'true'
     88:         req_body.attributes['xmlns'] = 'http://jabber.org/protocol/httpbind'
     89:         res_body = post(req_body)
     90:         unless res_body.name == 'body'
     91:           raise 'Response body is no <body/> element'
     92:         end
     93:
     94:         @streamid = res_body.attributes['authid']
     95:         @status = CONNECTED
     96:         @http_sid = res_body.attributes['sid']
     97:         @http_wait = res_body.attributes['wait'].to_i if res_body.attributes['wait']
     98:         @http_hold = res_body.attributes['hold'].to_i if res_body.attributes['hold']
     99:         @http_inactivity = res_body.attributes['inactivity'].to_i
    100:         @http_polling = res_body.attributes['polling'].to_i
    101:         @http_polling = 5 if @http_polling == 0
    102:         @http_requests = res_body.attributes['requests'].to_i
    103:         @http_requests = 1 if @http_requests == 0
    104:
    105:         receive_elements_with_rid(@http_rid, res_body.children)
    106:
    107:         @features_sem.run
    108:       end

ensure_one_pending_request()

Ensure that there is one pending request

Will be automatically called if you‘ve sent a stanza.

[Source]

         # File lib/xmpp4r/httpbinding/client.rb, line 128
    128:       def ensure_one_pending_request
    129:         return if is_disconnected?
    130:
    131:         if @lock.synchronize { @pending_requests } < 1
    132:           send_data('')
    133:         end
    134:       end

send(xml, &block)

Send a stanza, additionally with block

This method ensures a ‘jabber:client’ namespace for the stanza

[Source]

         # File lib/xmpp4r/httpbinding/client.rb, line 115
    115:       def send(xml, &block)
    116:         if xml.kind_of? REXML::Element
    117:           xml.add_namespace('jabber:client')
    118:         end
    119:
    120:         super
    121:       end

[Validate]