Class: Rex::Proto::MQTT::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/mqtt/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(sock, opts = {}) ⇒ Client

Returns a new instance of Client.


14
15
16
17
# File 'lib/rex/proto/mqtt/client.rb', line 14

def initialize(sock, opts = {})
  @sock = sock
  @opts = opts
end

Instance Method Details

#connectObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rex/proto/mqtt/client.rb', line 19

def connect
  connect_opts = {
    client_id: @opts[:client_id]
  }

  unless @opts[:username].blank?
    connect_opts[:username] = @opts[:username]
  end
  unless @opts[:password].blank?
    connect_opts[:password] = @opts[:password]
  end
  connect = ::MQTT::Packet::Connect.new(connect_opts).to_s
  @sock.put(connect)
  res = @sock.get_once(-1, @opts[:read_timeout])
  ::MQTT::Packet.parse(res)
end

#connect?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/rex/proto/mqtt/client.rb', line 36

def connect?
  connect.return_code.zero?
end

#disconnectObject


40
41
42
43
# File 'lib/rex/proto/mqtt/client.rb', line 40

def disconnect
  disconnect = ::MQTT::Packet::Disconnect.new().to_s
  @sock.put(disconnect)
end