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,
}); });
}
// Called when the client connects connectionCheckTimeout = setTimeout(checkConnection, 1000);
function onConnect() { }
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
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");
@ -41,25 +54,26 @@
client.subscribe(topic); client.subscribe(topic);
} }
// Subscribe to the requested topic // Subscribe to the requested topic
} }
// Called when the client loses its connection // Called when the client loses its connection
function onConnectionLost(responseObject) { function onConnectionLost(responseObject) {
document.getElementById("wrapper").classList.remove("wrapper-ok"); document.getElementById("wrapper").classList.remove("wrapper-ok");
document.getElementById("wrapper").classList.add("wrapper-error"); document.getElementById("wrapper").classList.add("wrapper-error");
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) {
var current=obj; var current=obj;
path.split('.').forEach(function(p){ current = current[p]; }); path.split('.').forEach(function(p){ current = current[p]; });
return current; return current;
} }
// Called when a message arrives // Called when a message arrives
function onMessageArrived(message) { function onMessageArrived(message) {
var views = document.getElementsByClassName(message.destinationName + "-value"); var views = document.getElementsByClassName(message.destinationName + "-value");
for (var i = 0 ; i < views.length; i++) { for (var i = 0 ; i < views.length; i++) {
var view = views.item(i); var view = views.item(i);
@ -88,34 +102,35 @@
widgets.item(i).classList.remove("widget-unset"); widgets.item(i).classList.remove("widget-unset");
} }
} }
// Called when the disconnection button is pressed // Called when the disconnection button is pressed
function startDisconnect() { function startDisconnect() {
client.disconnect(); client.disconnect();
document.getElementById("messages").innerHTML += '<span>Disconnected</span><br/>'; document.getElementById("messages").innerHTML += '<span>Disconnected</span><br/>';
updateScroll(); // Scroll to bottom of window updateScroll(); // Scroll to bottom of window
} }
// Updates #messages div to auto-scroll // Updates #messages div to auto-scroll
function updateScroll() { function updateScroll() {
var element = document.getElementById("messages"); var element = document.getElementById("messages");
element.scrollTop = element.scrollHeight; element.scrollTop = element.scrollHeight;
} }
function publish(topic, value) { function publish(topic, value) {
var message = new Paho.MQTT.Message(value); var message = new Paho.MQTT.Message(value);
message.destinationName = topic; message.destinationName = topic;
client.send(message); client.send(message);
} }
function updateContents(id, value) { function updateContents(id, value) {
elem = document.getElementById(id); elem = document.getElementById(id);
elem.innerHTML = value; elem.innerHTML = value;
} }
function init(brokerUri) { function init(brokerUri) {
startConnect(brokerUri); window.brokerUri = brokerUri
startConnect();
var sliders = document.getElementsByClassName("slider"); var sliders = document.getElementsByClassName("slider");
@ -145,4 +160,4 @@
} }
} }

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>