@@ -55,7 +55,7 @@ Client.__index = Client
5555-- <br/> `[mqtt[s]://][username[:password]@]hostname[:port]`
5656-- <br/>Any option specifically added to the options
5757-- table will take precedence over the option specified in this uri.
58- -- @tparam boolean opts.clean clean session start flag
58+ -- @tparam boolean|string opts.clean clean session start flag, use "first" to start clean only on first connect
5959-- @tparam [opt] string opts.protocol either `"mqtt"` or `"mqtts"`
6060-- @tparam [opt] string opts.username username for authorization on MQTT broker
6161-- @tparam [opt] string opts.password password for authorization on MQTT broker; not acceptable in absence of username
@@ -64,7 +64,7 @@ Client.__index = Client
6464-- @tparam [opt=4] number opts.version MQTT protocol version to use, either `4` (for MQTT v3.1.1) or `5` (for MQTT v5.0).
6565-- Also you may use special values `mqtt.v311` or `mqtt.v50` for this field.
6666-- @tparam [opt] string opts.id MQTT client ID, will be generated by luamqtt library if absent
67- -- @tparam [opt=false] boolean, table opts.secure use secure network connection, provided by the lua module set in `opts.ssl_module`.
67+ -- @tparam [opt=false] boolean| table opts.secure use secure network connection, provided by the lua module set in `opts.ssl_module`.
6868-- Set to true to select default parameters, check individual `mqtt.connectors` for supported options.
6969-- @tparam [opt] table opts.will will message table with required fields `{ topic="...", payload="..." }`
7070-- and optional fields `{ qos=0...2, retain=true/false }`
@@ -83,7 +83,7 @@ Client.__index = Client
8383--
8484-- local my_client = Client.create {
8585-- uri = "mqtts://broker.host.com",
86- -- clean = true ,
86+ -- clean = "first" ,
8787-- version = mqtt.v50,
8888-- }
8989function Client :__init (opts )
@@ -104,7 +104,7 @@ function Client:__init(opts)
104104 assert (value_type == " string" , " expecting uri to be a string" )
105105 a .uri = value
106106 elseif key == " clean" then
107- assert (value_type == " boolean" , " expecting clean to be a boolean" )
107+ assert (value_type == " boolean" or value == " first " , " expecting clean to be a boolean, or 'first' " )
108108 a .clean = value
109109 elseif key == " version" then
110110 assert (value_type == " number" , " expecting version to be a number" )
@@ -152,7 +152,7 @@ function Client:__init(opts)
152152
153153 -- check required arguments
154154 assert (a .uri , ' expecting uri="..." to create MQTT client' )
155- assert (a .clean ~= nil , " expecting clean=true or clean=false to create MQTT client" )
155+ assert (a .clean ~= nil , " expecting clean=true, clean=false, or clean='first' to create MQTT client" )
156156
157157 if not a .id then
158158 -- generate random client id
@@ -858,7 +858,7 @@ function Client:send_connect()
858858 local connect = self ._make_packet {
859859 type = packet_type .CONNECT ,
860860 id = opts .id ,
861- clean = opts .clean ,
861+ clean = not not opts .clean , -- force to boolean, in case "first"
862862 username = opts .username ,
863863 password = opts .password ,
864864 will = opts .will ,
@@ -1058,6 +1058,10 @@ function Client:handle_received_packet(packet)
10581058 log :info (" client '%s' connected successfully to '%s:%s'" , self .opts .id , conn .host , conn .port )
10591059
10601060 -- fire connect event
1061+ if self .opts .clean == " first" then
1062+ self .opts .clean = false -- reset clean flag to false, so next connection resumes previous session
1063+ log :debug (" client '%s'; switching clean flag to false (was 'first')" , self .opts .id )
1064+ end
10611065 self :handle (" connect" , packet , self )
10621066 self .first_connect = false
10631067 else
0 commit comments