Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for node-xmpp-bosh.

  • Loading branch information...
commit 4928ee071c771dbfb578c0272fb7ba54743ec690 1 parent ea954c9
@nchapman nchapman authored
Showing with 25 additions and 16 deletions.
  1. +25 −16 lib/ruby_bosh.rb
View
41 lib/ruby_bosh.rb
@@ -6,7 +6,7 @@
require 'timeout'
require 'system_timer' unless RUBY_VERSION =~ /1\.9/
-class RubyBOSH
+class RubyBOSH
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'
@@ -29,12 +29,12 @@ def self.fast_mode=(value)
end
attr_accessor :jid, :rid, :sid, :success
- def initialize(jid, pw, service_url, opts={})
+ def initialize(jid, pw, service_url, opts={})
@service_url = service_url
@jid, @pw = jid, pw
@host = jid.split("@").last
@success = false
- @timeout = opts[:timeout] || 3 #seconds
+ @timeout = opts[:timeout] || 3 #seconds
@headers = {"Content-Type" => "text/xml; charset=utf-8",
"Accept" => "text/xml"}
@wait = opts[:wait] || 5
@@ -51,7 +51,12 @@ def self.initialize_session(*args)
end
def connect
- initialize_bosh_session
+ response = initialize_bosh_session
+
+ unless response =~ /stream:features/
+ send_empty_body
+ end
+
if send_auth_request
@success = send_restart_request
unless @@fast_mode
@@ -80,7 +85,7 @@ def deliver(xml)
end
private
- def initialize_bosh_session
+ def initialize_bosh_session
response = deliver(construct_body(:wait => @wait, :to => @host,
:hold => @hold, :window => @window,
"xmpp:version" => '1.0'))
@@ -92,7 +97,7 @@ def construct_body(params={}, &block)
builder = Builder::XmlMarkup.new
parameters = {:rid => @rid, :xmlns => BOSH_XMLNS,
- "xmpp:version" => "1.0",
+ "xmpp:version" => "1.0",
"xmlns:xmpp" => "urn:xmpp:xbosh"}.merge(params)
if block_given?
@@ -102,10 +107,10 @@ def construct_body(params={}, &block)
end
end
- def send_auth_request
+ 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/,''),
+ auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}"
+ body.auth(Base64.encode64(auth_string).gsub(/\s/,''),
:xmlns => SASL_XMLNS, :mechanism => 'PLAIN')
end
@@ -120,16 +125,16 @@ def send_restart_request
def request_resource_binding
request = construct_body(:sid => @sid) do |body|
- body.iq(:id => "bind_#{rand(100000)}", :type => "set",
+ body.iq(:id => "bind_#{rand(100000)}", :type => "set",
:xmlns => "jabber:client") do |iq|
iq.bind(:xmlns => BIND_XMLNS) do |bind|
bind.resource("bosh_#{rand(10000)}")
end
end
end
-
+
response = deliver(request)
- response.include?("<jid>")
+ response.include?("<jid>")
end
def send_session_request
@@ -137,11 +142,15 @@ def send_session_request
body.iq(:xmlns => CLIENT_XMLNS, :type => "set",
:id => "sess_#{rand(100000)}") do |iq|
iq.session(:xmlns => SESSION_XMLNS)
- end
+ end
end
response = deliver(request)
- response.include?("body")
+ response.include?("body")
+ end
+
+ def send_empty_body
+ deliver(construct_body(:sid => @sid))
end
def parse(_response)
@@ -173,13 +182,13 @@ def recv(msg)
puts("Ruby-BOSH - RECV\n[#{now}]: #{msg}") if @@logging; msg
end
- def now
+ def now
Time.now.strftime("%a %b %d %H:%M:%S %Y")
end
end
if __FILE__ == $0
- p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
+ p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
"http://localhost:5280/http-bind")
end

0 comments on commit 4928ee0

Please sign in to comment.
Something went wrong with that request. Please try again.