Class: MiniMqtt::ConnectPacket
- Defined in:
- lib/mini_mqtt/connect_packet.rb
Instance Method Summary collapse
- #build_payload ⇒ Object
- #build_variable_header ⇒ Object
-
#initialize(options = {}) ⇒ ConnectPacket
constructor
A new instance of ConnectPacket.
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 = {} @user = [:user] @password = [:password] @client_id = [:client_id] @will_message = [:will_message] @will_topic = [:will_topic] @will_retain = [:will_retain] @will_qos = [:will_qos] || 0 @clean_session = [:clean_session] @keep_alive = [:keep_alive] end |
Instance Method Details
#build_payload ⇒ Object
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_header ⇒ Object
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 |