<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -6,13 +6,14 @@ shared 'eventmachine' do
   $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
 </diff>
      <filename>spec/spec_runner.rb</filename>
    </modified>
    <modified>
      <diff>@@ -37,4 +37,21 @@ describe 'XMPP4EM' do
 
     received.should == 'hello'
   end
-end
\ No newline at end of file
+
+  should 'fire disconnect callback and reconnect' do
+    user = XMPP4EM::Client.new('user@localhost', 'user', :auto_register =&gt; 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</diff>
      <filename>spec/xmpp4em_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -16,11 +16,11 @@ module XMPP4EM
   class NotConnected &lt; Exception; end
 
   class Connection &lt; 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 @@ module XMPP4EM
         @keepalive.cancel
         @keepalive = nil
       end
+      @client.on(:disconnect)
       log 'disconnected'
     end
 
+    def reconnect host = @host, port = @port
+      super
+    end
+
     def init
       send &quot;&lt;?xml version='1.0' ?&gt;&quot; unless @started
       @started = false
@@ -156,11 +161,12 @@ module XMPP4EM
       @id_callbacks  = {}
 
       @callbacks = {
-        :message   =&gt; [],
-        :presence  =&gt; [],
-        :iq        =&gt; [],
-        :exception =&gt; [],
-        :login     =&gt; []
+        :message    =&gt; [],
+        :presence   =&gt; [],
+        :iq         =&gt; [],
+        :exception  =&gt; [],
+        :login      =&gt; [],
+        :disconnect =&gt; []
       }
 
       @opts = { :auto_register =&gt; false }.merge(opts)
@@ -177,13 +183,17 @@ module XMPP4EM
 
     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 @@ module XMPP4EM
             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 @@ module XMPP4EM
 
       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, &amp;blk
-      @callbacks[type] &lt;&lt; blk
+    def on type, *args, &amp;blk
+      if blk
+        @callbacks[type] &lt;&lt; blk
+      else
+        @callbacks[type].each do |blk|
+          blk.call(*args)
+        end
+      end
     end
     
     def add_message_callback  (&amp;blk) on :message,   &amp;blk end</diff>
      <filename>xmpp4em.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>81e5d2d9899d66b053ab00a3814ab5b03ed473c5</id>
    </parent>
  </parents>
  <author>
    <name>Aman Gupta</name>
    <email>aman@tmm1.net</email>
  </author>
  <url>http://github.com/tmm1/xmpp4em/commit/0593bc2571223b660bd3e8f392a9929e63df6eb8</url>
  <id>0593bc2571223b660bd3e8f392a9929e63df6eb8</id>
  <committed-date>2008-07-07T14:36:59-07:00</committed-date>
  <authored-date>2008-07-07T14:36:59-07:00</authored-date>
  <message>add #reconnect and on(:disconnect) callback</message>
  <tree>0a89aaa00837d406adca2a1f857cd65af60a419f</tree>
  <committer>
    <name>Aman Gupta</name>
    <email>aman@tmm1.net</email>
  </committer>
</commit>
