Skip to content

Commit

Permalink
Add PR njh#128: Assume publish response as ping response.
Browse files Browse the repository at this point in the history
  • Loading branch information
phlegx committed Sep 5, 2023
1 parent 271ee63 commit 2497e6b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/mqtt/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/mqtt_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 2497e6b

Please sign in to comment.