Class: XMPP4EM::BaseClient

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/xmpp4em/base_client.rb

Direct Known Subclasses

Client, Component

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, logger = nil, opts = {}) ⇒ BaseClient

Returns a new instance of BaseClient.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xmpp4em/base_client.rb', line 23

def initialize user, pass, logger=nil,opts = {}
  @user = user
  @pass = pass
  @logger=logger
  @deferred_status =:succeeded
  @connection = nil
  @authenticated = false
  @opts = opts
  @auth_callback = nil
  @id_callbacks  = {}
  @on_stanza=nil
  @events_callbacks = {
    :message    => [],
    :presence   => [],
    :iq         => [],
    :exception  => [],
    :login      => [],
    :disconnect => [],
    :connected  => []
  }     
  
  on(:disconnect) do
    @deferred_status = nil
    @authenticated=false
  end         
  on(:login) do
    succeed
  end    
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



52
53
54
# File 'lib/xmpp4em/base_client.rb', line 52

def connection
  @connection
end

#userObject (readonly)

Returns the value of attribute user.



52
53
54
# File 'lib/xmpp4em/base_client.rb', line 52

def user
  @user
end

Instance Method Details

#add_iq_callback(&blk) ⇒ Object



119
# File 'lib/xmpp4em/base_client.rb', line 119

def add_iq_callback       (&blk) on :iq,        &blk end

#add_message_callback(&blk) ⇒ Object



117
# File 'lib/xmpp4em/base_client.rb', line 117

def add_message_callback  (&blk) on :message,   &blk end

#add_presence_callback(&blk) ⇒ Object



118
# File 'lib/xmpp4em/base_client.rb', line 118

def add_presence_callback (&blk) on :presence,  &blk end

#closeObject Also known as: disconnect



83
84
85
86
87
# File 'lib/xmpp4em/base_client.rb', line 83

def close
  @connection.close_connection_after_writing
  @deferred_status = nil
  @connection = nil
end

#connected?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/xmpp4em/base_client.rb', line 60

def connected?
  @connection and !@connection.error?
end

#on(type, *args, &blk) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/xmpp4em/base_client.rb', line 107

def on type, *args, &blk
  if blk
    @events_callbacks[type] << blk
  else
    @events_callbacks[type].each do |blk|
      blk.call(*args)
    end
  end
end

#on_exception(&blk) ⇒ Object



120
# File 'lib/xmpp4em/base_client.rb', line 120

def on_exception          (&blk) on :exception, &blk end

#receive(stanza) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xmpp4em/base_client.rb', line 90

def receive stanza
  if stanza.kind_of?(Jabber::XMPPStanza) and stanza.id and blk = @id_callbacks[ stanza.id ]
    @id_callbacks.delete stanza.id
    blk.call(stanza)
    return
  end
  
  return if receive_stanza(stanza)      
  return if @on_stanza && @on_stanza.call(stanza)  
  
  case stanza
    when Jabber::Message then  on(:message, stanza)
    when Jabber::Iq then  on(:iq, stanza)
    when Jabber::Presence then on(:presence, stanza)
  end
end

#reconnectObject



54
55
56
57
58
# File 'lib/xmpp4em/base_client.rb', line 54

def reconnect
  @connection.close_connection_after_writing
  @deferred_status = nil
  connect
end

#register_stanza(&blk) ⇒ Object



64
65
66
# File 'lib/xmpp4em/base_client.rb', line 64

def register_stanza &blk          
  @on_stanza = blk if block_given?
end

#send(data, safe = false, &blk) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/xmpp4em/base_client.rb', line 68

def send data, safe=false,  &blk
  
  if block_given? and data.is_a? Jabber::XMPPStanza
    if data.id.nil?
      data.id = Jabber::IdGenerator.instance.generate_id
    end
    @id_callbacks[ data.id ] = blk
  end
  if safe
    callback {   @connection.send(data) }
  else
    @connection.send(data)
  end
end