Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assume publish response as ping response #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/mqtt/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 @@ -74,7 +77,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 @@ -486,6 +490,7 @@ def handle_packet(packet)
if packet.class == MQTT::Packet::Publish
# Add to queue
@read_queue.push(packet)
@last_ping_response = Time.now if @assume_publish_response_as_pingresp
elsif packet.class == MQTT::Packet::Pingresp
@last_ping_response = Time.now
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 @@ -30,6 +30,7 @@
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 @@ -81,6 +82,15 @@
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