Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add #reconnect and on(:disconnect) callback

  • Loading branch information...
commit 0593bc2571223b660bd3e8f392a9929e63df6eb8 1 parent 81e5d2d
@tmm1 authored
Showing with 51 additions and 25 deletions.
  1. +2 −1  spec/spec_runner.rb
  2. +18 −1 spec/xmpp4em_spec.rb
  3. +31 −23 xmpp4em.rb
View
3  spec/spec_runner.rb
@@ -6,13 +6,14 @@
$bacon_thread = Thread.current
def wait
Thread.stop
- EM.add_timer(10) do
+ @timer = EM::Timer.new(10) do
wake
should.flunk('waited too long')
end
end
def wake
$bacon_thread.wakeup
+ @timer.cancel if @timer
end
end
View
19 spec/xmpp4em_spec.rb
@@ -37,4 +37,21 @@
received.should == 'hello'
end
-end
+
+ should 'fire disconnect callback and reconnect' do
+ user = XMPP4EM::Client.new('user@localhost', 'user', :auto_register => true)
+ user.on(:disconnect){ wake }
+ user.connect 'localhost', 5333 # invalid port
+ wait
+
+ user.should.not.be.connected?
+
+ user.instance_variable_get('@callbacks')[:disconnect] = []
+ user.connection.port = 5222
+ user.on(:login){ wake }
+ user.reconnect
+ wait
+
+ user.should.be.connected?
+ end
+end
View
54 xmpp4em.rb
@@ -16,11 +16,11 @@ module XMPP4EM
class NotConnected < Exception; end
class Connection < EventMachine::Connection
- def initialize host
- @host = host
+ def initialize host, port
+ @host, @port = host, port
@client = nil
end
- attr_accessor :client
+ attr_accessor :client, :host, :port
def connection_completed
log 'connected'
@@ -81,9 +81,14 @@ def unbind
@keepalive.cancel
@keepalive = nil
end
+ @client.on(:disconnect)
log 'disconnected'
end
+ def reconnect host = @host, port = @port
+ super
+ end
+
def init
send "<?xml version='1.0' ?>" unless @started
@started = false
@@ -156,11 +161,12 @@ def initialize user, pass, opts = {}
@id_callbacks = {}
@callbacks = {
- :message => [],
- :presence => [],
- :iq => [],
- :exception => [],
- :login => []
+ :message => [],
+ :presence => [],
+ :iq => [],
+ :exception => [],
+ :login => [],
+ :disconnect => []
}
@opts = { :auto_register => false }.merge(opts)
@@ -177,13 +183,17 @@ def jid
def connect host = jid.domain, port = 5222
EM.run {
- EM.connect host, port, Connection, host do |conn|
+ EM.connect host, port, Connection, host, port do |conn|
@connection = conn
conn.client = self
end
}
end
+ def reconnect
+ @connection.reconnect
+ end
+
def connected?
@connection and !@connection.error?
end
@@ -268,9 +278,7 @@ def receive stanza
send(iq){ |reply|
if reply.type == :result
- @callbacks[:login].each do |blk|
- blk.call(stanza)
- end
+ on(:login, stanza)
end
}
end
@@ -291,25 +299,25 @@ def receive stanza
case stanza
when Jabber::Message
- @callbacks[:message].each do |blk|
- blk.call(stanza)
- end
+ on(:message, stanza)
when Jabber::Iq
- @callbacks[:iq].each do |blk|
- blk.call(stanza)
- end
+ on(:iq, stanza)
when Jabber::Presence
- @callbacks[:presence].each do |blk|
- blk.call(stanza)
- end
+ on(:presence, stanza)
end
end
- def on type, &blk
- @callbacks[type] << blk
+ def on type, *args, &blk
+ if blk
+ @callbacks[type] << blk
+ else
+ @callbacks[type].each do |blk|
+ blk.call(*args)
+ end
+ end
end
def add_message_callback (&blk) on :message, &blk end
Please sign in to comment.
Something went wrong with that request. Please try again.