Reconnect to MQTT broker automatically

This commit is contained in:
0xee 2019-11-09 09:10:06 +01:00
parent 2d40ab72ff
commit 4e6b2307dd
2 changed files with 142 additions and 129 deletions

View File

@ -1,18 +1,15 @@
// Called after form input is processed // Called after form input is processed
function startConnect(brokerUri) { function startConnect() {
// Generate a random client ID // Generate a random client ID
clientId = "clientId-" + parseInt(Math.random() * 100); clientId = "clientId-" + parseInt(Math.random() * 1000000);
document.getElementById("wrapper").classList.add("wrapper-error"); document.getElementById("wrapper").classList.add("wrapper-error");
document.getElementById("wrapper").classList.remove("wrapper-ok"); document.getElementById("wrapper").classList.remove("wrapper-ok");
// Print output for the user in the messages div // Print output for the user in the messages div
document.getElementById("messages").innerHTML += log('Connecting to: ' + window.brokerUri + ', client ID: ' + clientId);
'<span>Connecting to: ' + brokerUri+ '</span><br/>';
document.getElementById("messages").innerHTML +=
'<span>Using the following client value: ' + clientId + '</span><br/>';
// Initialize new Paho client connection // Initialize new Paho client connection
client = new Paho.MQTT.Client(brokerUri, clientId=clientId); client = new Paho.MQTT.Client(window.brokerUri, clientId=clientId);
// Set callback handlers // Set callback handlers
client.onConnectionLost = onConnectionLost; client.onConnectionLost = onConnectionLost;
@ -22,10 +19,26 @@
client.connect({ client.connect({
onSuccess: onConnect, onSuccess: onConnect,
}); });
connectionCheckTimeout = setTimeout(checkConnection, 1000);
}
function log(msg) {
console.log(msg);
//document.getElementById("messages").innerHTML += '<span>' + msg + '</span><br/>';
}
function checkConnection(){
clearTimeout(connectionCheckTimeout);
if (!client.isConnected()) {
//log("Error connecting to broker");
startConnect();
}
} }
// Called when the client connects // Called when the client connects
function onConnect() { function onConnect() {
clearTimeout(connectionCheckTimeout);
document.getElementById("wrapper").classList.remove("wrapper-error"); document.getElementById("wrapper").classList.remove("wrapper-error");
document.getElementById("wrapper").classList.add("wrapper-ok"); document.getElementById("wrapper").classList.add("wrapper-ok");
var subWidgets = document.getElementsByClassName("subscriber"); var subWidgets = document.getElementsByClassName("subscriber");
@ -50,6 +63,7 @@
if (responseObject.errorCode !== 0) { if (responseObject.errorCode !== 0) {
console.log("onConnectionLost: " + responseObject.errorMessage); console.log("onConnectionLost: " + responseObject.errorMessage);
} }
startConnect();
} }
function getByPath(obj, path) { function getByPath(obj, path) {
@ -115,7 +129,8 @@
} }
function init(brokerUri) { function init(brokerUri) {
startConnect(brokerUri); window.brokerUri = brokerUri
startConnect();
var sliders = document.getElementsByClassName("slider"); var sliders = document.getElementsByClassName("slider");

View File

@ -26,8 +26,6 @@
</div> </div>
<br/> <br/>
<div id="messages"></div> <div id="messages"></div>
<input type="button" onclick="startConnect()" value="Connect">
<input type="button" onclick="startDisconnect()" value="Disconnect">
</div> </div>
<br/> <br/>
</div> </div>