Class: MiniMqtt::ConnectPacket

Inherits:
Packet
  • Object
show all
Defined in:
lib/mini_mqtt/connect_packet.rb

Instance Method Summary collapse

Methods inherited from Packet

#decode, #encode, #flags, get_packet_class, packet_type_id

Methods included from BinHelper

#flag_byte, #mqtt_utf8_encode, #read_mqtt_encoded_string, #read_ushort, #uchar, #ushort

Constructor Details

#initialize(options = {}) ⇒ ConnectPacket

Returns a new instance of ConnectPacket.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mini_mqtt/connect_packet.rb', line 5

def initialize options = {}
  @user = options[:user]
  @password = options[:password]
  @client_id = options[:client_id]
  @will_message = options[:will_message]
  @will_topic = options[:will_topic]
  @will_retain = options[:will_retain]
  @will_qos = options[:will_qos] || 0
  @clean_session = options[:clean_session]
  @keep_alive = options[:keep_alive]
end

Instance Method Details

#build_payloadObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/mini_mqtt/connect_packet.rb', line 33

def build_payload
  payload = mqtt_utf8_encode(@client_id)
  if @will_message
    payload << mqtt_utf8_encode(@will_topic)
    payload << mqtt_utf8_encode(@will_message)
  end
  payload << mqtt_utf8_encode(@user) if @user
  payload << mqtt_utf8_encode(@password) if @password
  payload
end

#build_variable_headerObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mini_mqtt/connect_packet.rb', line 17

def build_variable_header
  # Protocol name
  header = ushort(4) # length of name
  header << 'MQTT' # name
  # Protocol level
  header << uchar(4)
  # Flags
  byte = flag_byte [ @user, @password, @will_retain, nil, nil,
                        @will_message, @clean_session, nil]
  byte |= @will_qos << 3
  header << uchar(byte)
  #Keepalive
  header << ushort(@keep_alive)
  header
end