lib/mqtt/client.rb in mqtt-0.4.0 vs lib/mqtt/client.rb in mqtt-0.5.0
- old
+ new
@@ -8,11 +8,11 @@
attr_accessor :host
# Port number of the remote server
attr_accessor :port
- # The version number of the MQTT protocol to use (default 3.1.0)
+ # The version number of the MQTT protocol to use (default 3.1.1)
attr_accessor :version
# Set to true to enable SSL/TLS encrypted communication
#
# Set to a symbol to use a specific variant of SSL/TLS.
@@ -62,11 +62,11 @@
# Default attribute values
ATTR_DEFAULTS = {
:host => nil,
:port => nil,
- :version => '3.1.0',
+ :version => '3.1.1',
:keep_alive => 15,
:clean_session => true,
:client_id => nil,
:ack_timeout => 5,
:username => nil,
@@ -262,10 +262,16 @@
ssl_context.ssl_version = @ssl
end
@socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
@socket.sync_close = true
+
+ # Set hostname on secure socket for Server Name Indication (SNI)
+ if @socket.respond_to?(:hostname=)
+ @socket.hostname = @host
+ end
+
@socket.connect
else
@socket = tcp_socket
end
@@ -391,19 +397,21 @@
# Or can be used with a block to keep processing messages:
# client.get('test') do |topic,payload|
# # Do stuff here
# end
#
- def get(topic=nil)
+ def get(topic=nil, options={})
if block_given?
get_packet(topic) do |packet|
- yield(packet.topic, packet.payload)
+ yield(packet.topic, packet.payload) unless packet.retain && options[:omit_retained]
end
else
- # Wait for one packet to be available
- packet = get_packet(topic)
- return packet.topic, packet.payload
+ loop do
+ # Wait for one packet to be available
+ packet = get_packet(topic)
+ return packet.topic, packet.payload unless packet.retain && options[:omit_retained]
+ end
end
end
# Return the next packet object received from the MQTT server.
# An optional topic can be given to subscribe to.
@@ -498,11 +506,11 @@
# Ignore all other packets
# FIXME: implement responses for QoS 2
end
def keep_alive!
- if @keep_alive > 0
+ if @keep_alive > 0 && connected?
response_timeout = (@keep_alive * 1.5).ceil
if Time.now >= @last_ping_request + @keep_alive
packet = MQTT::Packet::Pingreq.new
send_packet(packet)
@last_ping_request = Time.now
@@ -528,10 +536,13 @@
)
end
# Check the return code
if packet.return_code != 0x00
+ # 3.2.2.3 If a server sends a CONNACK packet containing a non-zero
+ # return code it MUST then close the Network Connection
+ @socket.close
raise MQTT::ProtocolException.new(packet.return_msg)
end
end
end
@@ -558,11 +569,11 @@
end
{
:host => uri.host,
:port => uri.port || nil,
- :username => uri.user,
- :password => uri.password,
+ :username => uri.user ? URI.unescape(uri.user) : nil,
+ :password => uri.password ? URI.unescape(uri.password) : nil,
:ssl => ssl
}
end
def next_packet_id