spec/support/fake_mqtt_server.rb
# Given a client socket, process MQTT packets from the client
Given a client socket, process MQTT packets from the client
Given a client socket process MQTT packets from the client
def handle_client(client)
    loop do
      packet = MQTT::Packet.read(client)
      logger.debug packet.inspect
      case packet
        when MQTT::Packet::Connect
          client.write MQTT::Packet::Connack.new(:return_code => 0)
        when MQTT::Packet::Publish
          client.write packet
          @last_publish = packet
        when MQTT::Packet::Subscribe
          client.write MQTT::Packet::Suback.new(
            :message_id => packet.message_id,
            :granted_qos => 0
          )
          topic = packet.topics[0][0]
          case @times
          when 1, ->x { x >= 3 }
            client.write MQTT::Packet::Publish.new(
              :topic => topic,
              :payload => "did you know about #{topic}",
              :retain => true
            )
            client.write MQTT::Packet::Publish.new(
              :topic => topic,
              :payload => "hello #{topic}",
              :retain => true
            )
          when 2
            client.write MQTT::Packet::Publish.new(
              :topic => topic,
              :payload => "hello #{topic}",
              :retain => true
            )
            client.write MQTT::Packet::Publish.new(
              :topic => topic,
              :payload => "did you know about #{topic}",
              :retain => false
            )
          end
        when MQTT::Packet::Pingreq
          client.write MQTT::Packet::Pingresp.new
          @pings_received += 1
        when MQTT::Packet::Disconnect
          client.close
        break
      end
    end
    rescue MQTT::ProtocolException => e
      logger.warn "Protocol error, closing connection: #{e}"
      client.close
  end
def handle_client ( client ) loop do packet = MQTT :: Packet . read ( client ) logger . debug packet . inspect case packet when MQTT :: Packet :: Connect client . write MQTT :: Packet :: Connack . new ( :return_code => 0 ) when MQTT :: Packet :: Publish client . write packet @last_publish = packet when MQTT :: Packet :: Subscribe client . write MQTT :: Packet :: Suback . new ( :message_id => packet . message_id , :granted_qos => 0 ) topic = packet . topics [ 0 ] [ 0 ] case @times when 1 , -> x { x >= 3 } client . write MQTT :: Packet :: Publish . new ( :topic => topic , :payload => "did you know about #{topic}" , :retain => true ) client . write MQTT :: Packet :: Publish . new ( :topic => topic , :payload => "hello #{topic}" , :retain => true ) when 2 client . write MQTT :: Packet :: Publish . new ( :topic => topic , :payload => "hello #{topic}" , :retain => true ) client . write MQTT :: Packet :: Publish . new ( :topic => topic , :payload => "did you know about #{topic}" , :retain => false ) end when MQTT :: Packet :: Pingreq client . write MQTT :: Packet :: Pingresp . new @pings_received += 1 when MQTT :: Packet :: Disconnect client . close break end end rescue MQTT :: ProtocolException => e logger . warn "Protocol error, closing connection: #{e}" client . close end
Given a client socket, process MQTT packets from the client
Given a client socket process MQTT packets from the client
# Deliver retained messages # Deliver a still retained message # Deliver a fresh message
client
<not_specific>