class MQTT::Packet::Subscribe
Class representing an MQTT Client Subscribe packet
def encode_body
def encode_body raise 'no topics given when serialising packet' if @topics.empty? body = encode_short(@id) topics.each do |item| body += encode_string(item[0]) body += encode_bytes(item[1]) end body end
def initialize(args = {})
def initialize(args = {}) super(ATTR_DEFAULTS.merge(args)) end
def inspect
def inspect _str = "\#<#{self.class}: 0x%2.2X, %s>" % [ id, topics.map { |t| "'#{t[0]}':#{t[1]}" }.join(', ') ] end
def parse_body(buffer)
def parse_body(buffer) super(buffer) @id = shift_short(buffer) @topics = [] while buffer.bytesize > 0 topic_name = shift_string(buffer) topic_qos = shift_byte(buffer) @topics << [topic_name, topic_qos] end end
def topics=(value)
packet.topics = {'a/b' => 0, 'c/d' => 1}
packet.topics = [['a/b',0], ['c/d',1]]
packet.topics = ['a/b', 'c/d']
packet.topics = 'a/b'
For example:
* Hash: subscribe to multiple topics where the key is the topic and the value is the QoS level
* Array: subscribe to multiple topics with QoS 0
* String: subscribe to one topic with QoS 0
The topics parameter should be one of the following:
Set one or more topic filters for the Subscribe packet
def topics=(value) # Get input into a consistent state input = value.is_a?(Array) ? value.flatten : [value] @topics = [] until input.empty? item = input.shift if item.is_a?(Hash) # Convert hash into an ordered array of arrays @topics += item.sort elsif item.is_a?(String) # Peek at the next item in the array, and remove it if it is an integer if input.first.is_a?(Integer) qos = input.shift @topics << [item, qos] else @topics << [item, 0] end else # Meh? raise "Invalid topics input: #{value.inspect}" end end @topics end
def validate_flags
- Private: -
def validate_flags return if @flags == [false, true, false, false] raise ProtocolException, 'Invalid flags in SUBSCRIBE packet header' end