lib/mqtt/client.rb in mqtt-0.0.7 vs lib/mqtt/client.rb in mqtt-0.0.8
- old
+ new
@@ -6,10 +6,14 @@
attr_accessor :clean_session # Set the 'Clean Session' flag when connecting?
attr_accessor :client_id # Client Identifier
attr_accessor :ack_timeout # Number of seconds to wait for acknowledgement packets
attr_accessor :username # Username to authenticate to the broker with
attr_accessor :password # Password to authenticate to the broker with
+ attr_accessor :will_topic # The topic that the Will message is published to
+ attr_accessor :will_payload # Contents of message that is sent by broker when client disconnect
+ attr_accessor :will_qos # The QoS level of the will message sent by the broker
+ attr_accessor :will_retain # If the Will message should be retain by the broker after it is sent
# OLD deprecated clean_start
alias :clean_start :clean_session
alias :clean_start= :clean_session=
@@ -23,11 +27,15 @@
:keep_alive => 15,
:clean_session => true,
:client_id => nil,
:ack_timeout => 5,
:username => nil,
- :password => nil
+ :password => nil,
+ :will_topic => nil,
+ :will_payload => nil,
+ :will_qos => 0,
+ :will_retain => false
}
# Create and connect a new MQTT Client
# Accepts the same arguments as creating a new client.
# If a block is given, then it will be executed before disconnecting again.
@@ -41,10 +49,28 @@
client = MQTT::Client.new(*args)
client.connect(&block)
return client
end
+ # Generate a random client identifier
+ # (using the characters 0-9 and a-z)
+ def self.generate_client_id(prefix='ruby_', length=16)
+ str = prefix.dup
+ length.times do
+ num = rand(36)
+ if (num<10)
+ # Number
+ num += 48
+ else
+ # Letter
+ num += 87
+ end
+ str += num.chr
+ end
+ return str
+ end
+
# Create a new MQTT Client instance
#
# Examples:
# client = MQTT::Client.new('myserver.example.com')
# client = MQTT::Client.new('myserver.example.com', 18830)
@@ -77,17 +103,24 @@
@read_queue = Queue.new
@read_thread = nil
@write_semaphore = Mutex.new
end
+ def set_will(topic, payload, retain=false, qos=0)
+ self.will_topic = topic
+ self.will_payload = payload
+ self.will_retain = retain
+ self.will_qos = qos
+ end
+
# Connect to the MQTT broker
# If a block is given, then yield to that block and then disconnect again.
def connect(clientid=nil)
if !clientid.nil?
@client_id = clientid
elsif @clientid.nil?
- @client_id = random_letters(16)
+ @client_id = MQTT::Client.generate_client_id
@clean_session = true
end
if not connected?
# Create network socket
@@ -97,11 +130,15 @@
packet = MQTT::Packet::Connect.new(
:clean_session => @clean_session,
:keep_alive => @keep_alive,
:client_id => @client_id,
:username => @username,
- :password => @password
+ :password => @password,
+ :will_topic => @will_topic,
+ :will_payload => @will_payload,
+ :will_qos => @will_qos,
+ :will_retain => @will_retain
)
# Send packet
send_packet(packet)
@@ -280,25 +317,8 @@
# Only allow one thread to write to socket at a time
@write_semaphore.synchronize do
@socket.write(data)
end
- end
-
- # Generate a string of random letters (0-9,a-z)
- def random_letters(count)
- str = ''
- count.times do
- num = rand(36)
- if (num<10)
- # Number
- num += 48
- else
- # Letter
- num += 87
- end
- str += num.chr
- end
- return str
end
end