Sha256: daf9445df392d7791fd8775af3a3000437626b0728bde1264aecf86b1a8d6e5b

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'blather/client'

module Adhearsion
  class XMPP
    module Connection

      class << self

        attr_accessor :client

        ##
        # Open the XMPP connection
        #
        # @param [String] jid the client/component JID to connect to
        # @param [String] password
        # @param [String] server
        # @param [Integer] port
        def start(jid, password, server, port)
          Blather.logger = logger
          setup_client_object jid, password, server, port
          register_event_namespaces
          register_default_client_handlers
          Adhearsion::Events.register_callback(:after_initialized) do
            connect
          end
        end

        # Close the XMPP connection
        def stop
          shutdown
        end

        private

        def setup_client_object(jid, password, server, port)
          self.client = Blather::Client.setup(jid, password, server, port)
        end

        def connect
          EventMachine.run {client.connect}
        end

        def register_event_namespaces
          Adhearsion::Events.register_namespace_name "/xmpp"
        end

        def register_default_client_handlers
          client.register_handler(:ready) do
            logger.info "Connected to XMPP server! Send messages to #{client.jid.stripped}."
          end

          client.register_handler(:disconnected) do
            if Adhearsion.status == :running
              logger.warn "XMPP Disconnected. Reconnecting."
              connect
            end
            # TODO: fix this to reconnect XMPP cleanly
          end
        end

      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adhearsion-xmpp-1.0.0 lib/adhearsion/xmpp/connection.rb
adhearsion-xmpp-0.2.1 lib/adhearsion/xmpp/connection.rb
adhearsion-xmpp-0.2.0 lib/adhearsion/xmpp/connection.rb