Improve reconnect robustness
This commit is contained in:
parent
ac284ffb1f
commit
a2a42acc35
@ -1,7 +1,11 @@
|
|||||||
// Called after form input is processed
|
// Called after form input is processed
|
||||||
function startConnect() {
|
function startConnect() {
|
||||||
|
if (connecting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connecting = true;
|
||||||
// Generate a random client ID
|
// Generate a random client ID
|
||||||
clientId = "clientId-" + parseInt(Math.random() * 1000000);
|
clientId = navigator.userAgent + "-" + 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");
|
||||||
|
|
||||||
@ -18,6 +22,7 @@ function startConnect() {
|
|||||||
// Connect the client, if successful, call onConnect function
|
// Connect the client, if successful, call onConnect function
|
||||||
client.connect({
|
client.connect({
|
||||||
onSuccess: onConnect,
|
onSuccess: onConnect,
|
||||||
|
timeout: 2
|
||||||
});
|
});
|
||||||
|
|
||||||
connectionCheckTimeout = setTimeout(checkConnection, 2000);
|
connectionCheckTimeout = setTimeout(checkConnection, 2000);
|
||||||
@ -25,12 +30,13 @@ function startConnect() {
|
|||||||
|
|
||||||
function log(msg) {
|
function log(msg) {
|
||||||
console.log(msg);
|
console.log(msg);
|
||||||
//document.getElementById("messages").innerHTML += '<span>' + msg + '</span><br/>';
|
document.getElementById("messages").innerHTML += '<span>' + msg + '</span><br/>';
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkConnection(){
|
function checkConnection(){
|
||||||
clearTimeout(connectionCheckTimeout);
|
clearTimeout(connectionCheckTimeout);
|
||||||
if (!client.isConnected()) {
|
if (!client.isConnected()) {
|
||||||
|
connecting = false;
|
||||||
//log("Error connecting to broker");
|
//log("Error connecting to broker");
|
||||||
startConnect();
|
startConnect();
|
||||||
}
|
}
|
||||||
@ -38,6 +44,7 @@ function checkConnection(){
|
|||||||
|
|
||||||
// Called when the client connects
|
// Called when the client connects
|
||||||
function onConnect() {
|
function onConnect() {
|
||||||
|
connecting = false;
|
||||||
clearTimeout(connectionCheckTimeout);
|
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");
|
||||||
@ -130,8 +137,8 @@ function updateContents(id, value) {
|
|||||||
|
|
||||||
function init(brokerUri) {
|
function init(brokerUri) {
|
||||||
window.brokerUri = brokerUri
|
window.brokerUri = brokerUri
|
||||||
|
connecting = false;
|
||||||
startConnect();
|
startConnect();
|
||||||
|
|
||||||
var sliders = document.getElementsByClassName("slider");
|
var sliders = document.getElementsByClassName("slider");
|
||||||
|
|
||||||
for (var i = 0 ; i < sliders.length; i++) {
|
for (var i = 0 ; i < sliders.length; i++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user