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.



20
21
22
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
# File 'lib/xmpp4em/base_client.rb', line 20

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

#authenticatedObject (readonly) Also known as: authenticated?

Returns the value of attribute authenticated.



17
18
19
# File 'lib/xmpp4em/base_client.rb', line 17

def authenticated
  @authenticated
end

#connectionObject (readonly)

Returns the value of attribute connection.



50
51
52
# File 'lib/xmpp4em/base_client.rb', line 50

def connection
  @connection
end

#userObject (readonly)

Returns the value of attribute user.



50
51
52
# File 'lib/xmpp4em/base_client.rb', line 50

def user
  @user
end

Instance Method Details

#add_iq_callback(&blk) ⇒ Object



126
127
128
# File 'lib/xmpp4em/base_client.rb', line 126

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

#add_message_callback(&blk) ⇒ Object



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

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

#add_presence_callback(&blk) ⇒ Object



122
123
124
# File 'lib/xmpp4em/base_client.rb', line 122

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

#closeObject Also known as: disconnect



79
80
81
82
83
# File 'lib/xmpp4em/base_client.rb', line 79

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

#connected?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/xmpp4em/base_client.rb', line 58

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

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



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

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



130
131
132
# File 'lib/xmpp4em/base_client.rb', line 130

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

#receive(stanza) ⇒ Object



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

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::Presence then
      on(:presence, stanza)
    when Jabber::Iq then
      on(:iq, stanza)
    when Jabber::Message then
      on(:message, stanza)
  end
end

#reconnectObject



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

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

#register_stanza(&blk) ⇒ Object



62
63
64
# File 'lib/xmpp4em/base_client.rb', line 62

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

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xmpp4em/base_client.rb', line 66

def send data, safe=false, &blk
  if data.is_a? ::Jabber::XMPPStanza
    data.id = ::Jabber::IdGenerator.instance.generate_id if data.id.nil?
    @id_callbacks[data.id] = blk if block_given?
  end

  if safe
    callback { @connection.send(data) }
  else
    @connection.send(data)
  end
end