From 2497e6bd98cd32e69675da067c7545e395481bef Mon Sep 17 00:00:00 2001 From: Egon Zemmer Date: Tue, 5 Sep 2023 20:39:04 +0200 Subject: [PATCH] Add PR #128: Assume publish response as ping response. --- lib/mqtt/client.rb | 7 ++++++- spec/mqtt_client_spec.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/mqtt/client.rb b/lib/mqtt/client.rb index 8b81ebf..dfb3614 100644 --- a/lib/mqtt/client.rb +++ b/lib/mqtt/client.rb @@ -57,6 +57,9 @@ class Client # Last ping response time attr_reader :last_ping_response + # Assume publish response as ping response + attr_accessor :assume_publish_response_as_pingresp + # Timeout between select polls (in seconds) SELECT_TIMEOUT = 0.5 @@ -75,7 +78,8 @@ class Client :will_payload => nil, :will_qos => 0, :will_retain => false, - :ssl => false + :ssl => false, + :assume_publish_response_as_pingresp => false } # Create and connect a new MQTT Client @@ -492,6 +496,7 @@ def handle_packet(packet) if packet.class == MQTT::Packet::Publish # Add to queue @read_queue.push(packet) + @last_ping_response = current_time if @assume_publish_response_as_pingresp elsif packet.class == MQTT::Packet::Pingresp @last_ping_response = current_time elsif packet.class == MQTT::Packet::Puback diff --git a/spec/mqtt_client_spec.rb b/spec/mqtt_client_spec.rb index 5ae92ab..f9bb035 100644 --- a/spec/mqtt_client_spec.rb +++ b/spec/mqtt_client_spec.rb @@ -41,6 +41,7 @@ def now expect(client.port).to eq(1883) expect(client.version).to eq('3.1.1') expect(client.keep_alive).to eq(15) + expect(client.assume_publish_response_as_pingresp).to be_falsey end it "with a single string argument, it should use it has the host" do @@ -92,6 +93,15 @@ def now expect(client.keep_alive).to eq(65) end + it "with a combination of a host name, port and a hash of settings with different pattern" do + client = MQTT::Client.new('localhost', 1888, :keep_alive => 65, + :assume_publish_response_as_pingresp => true) + expect(client.host).to eq('localhost') + expect(client.port).to eq(1888) + expect(client.keep_alive).to eq(65) + expect(client.assume_publish_response_as_pingresp).to be_truthy + end + it "with a mqtt:// URI containing just a hostname" do client = MQTT::Client.new(URI.parse('mqtt://mqtt.example.com')) expect(client.host).to eq('mqtt.example.com')