From 834e96d0b21a0c445edd42bf6f115bc4f94ddf58 Mon Sep 17 00:00:00 2001 From: nidayand Date: Wed, 4 Oct 2017 17:16:47 +0200 Subject: [PATCH] Changed the reconnect routine and added no-cache to html --- motor_on_a_roller_blind-ws.ino | 4 ++-- ui-template.html | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/motor_on_a_roller_blind-ws.ino b/motor_on_a_roller_blind-ws.ino index 5a34dea..e718b36 100644 --- a/motor_on_a_roller_blind-ws.ino +++ b/motor_on_a_roller_blind-ws.ino @@ -10,7 +10,7 @@ #include -String version = "1.1"; +String version = "1.1.1"; //Configure Default Settings String APid = "BlindsConnectAP"; @@ -423,7 +423,7 @@ void loop(void) String s; if (req == "/") { - s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n Home Settings About \r\n\r\n"; + s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n Home Settings About \r\n\r\n"; s.replace("{VERSION}","V"+version); s.replace("{NAME}",String(config_name)); Serial.println("Sending 200"); diff --git a/ui-template.html b/ui-template.html index 3b97a39..f181eaf 100644 --- a/ui-template.html +++ b/ui-template.html @@ -1,6 +1,9 @@ + + + @@ -58,14 +61,20 @@ }); var websocket; + var timeOut; + function retry(){ + clearTimeout(timeOut); + timeOut = setTimeout(function(){ + websocket=null; init();},2000); + }; function init(){ - websocket = null; ons.notification.toast({message: 'Connecting...', timeout: 1000}); try{ websocket = new WebSocket(wsUri); + websocket.onclose = function () {}; websocket.onerror = function(evt) { ons.notification.toast({message: 'Cannot connect to device', timeout: 2000}); - setTimeout(function(){init();},5000); + retry(); }; websocket.onopen = function(evt) { ons.notification.toast({message: 'Connected to device', timeout: 2000}); @@ -73,7 +82,7 @@ }; websocket.onclose = function(evt) { ons.notification.toast({message: 'Disconnected. Retrying', timeout: 2000}); - setTimeout(function(){init();},5000); + retry(); }; websocket.onmessage = function(evt) { try{ @@ -88,7 +97,7 @@ }; } catch (e){ ons.notification.toast({message: 'Cannot connect to device. Retrying...', timeout: 2000}); - setTimeout(function(){init();},5000); + retry(); }; }; function doSend(msg){ @@ -97,6 +106,11 @@ } }; window.addEventListener("load", init, false); + window.onbeforeunload = function() { + if (websocket && websocket.readyState == 1){ + websocket.close(); + }; + };