class MiniMqtt::ConnectPacket

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

build_payload() click to toggle source
# 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_header() click to toggle source
# 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