add keepalive

This commit is contained in:
ton
2023-10-07 12:57:41 +07:00
parent a65cda46a3
commit 4ef3da41ab
6 changed files with 965 additions and 4 deletions

View File

@@ -131,7 +131,7 @@ Base.@kwdef mutable struct mqttClient
subStatus::Bool= false
boottime::DateTime= Dates.now()
retainedMsgCleared= false
keepalive::Int= 60
end
function mqttClient(kwargs::NamedTuple)
@@ -149,7 +149,7 @@ function mqttClient(kwargs::NamedTuple)
s.clientInstance = Mosquitto.Client()
end
# connect
# connect to broker for the 1st time
mqttConnect!(s)
return s
@@ -162,7 +162,7 @@ function mqttConnect!(client::mqttClient)
password=client.password)
client.connectStatus = result == 0 ? true : false
else
result = Mosquitto.connect(client.clientInstance, client.broker, client.port)
result = Mosquitto.connect(client.clientInstance, client.broker, client.port, keepalive=client.keepalive)
client.connectStatus = result == 0 ? true : false
end
end
@@ -209,7 +209,7 @@ function mqttReconnect!(client::mqttClient)
Mosquitto.disconnect(client.clientInstance) # disconnect to release port before attemping new connection
if client.userName === nothing # check if the member needs username/password
result = Mosquitto.connect(client.clientInstance, client.broker, client.port)
result = Mosquitto.connect(client.clientInstance, client.broker, client.port, keepalive=client.keepalive)
client.connectStatus = result == 0 ? true : false
else
result = Mosquitto.connect(client.clientInstance, client.broker, client.port,