svghmi/svghmi.js
changeset 3683 bbcbb1bba9f1
parent 3680 20f9f0c36ad6
child 3685 570a738239f4
equal deleted inserted replaced
3682:c613afdab571 3683:bbcbb1bba9f1
   605 // show main page
   605 // show main page
   606 switch_page(default_page);
   606 switch_page(default_page);
   607 
   607 
   608 var reconnect_delay = 0;
   608 var reconnect_delay = 0;
   609 var periodic_reconnect_timer;
   609 var periodic_reconnect_timer;
       
   610 var force_reconnect = false;
   610 
   611 
   611 // Once connection established
   612 // Once connection established
   612 function ws_onopen(evt) {
   613 function ws_onopen(evt) {
   613     // Work around memory leak with websocket on QtWebEngine
   614     // Work around memory leak with websocket on QtWebEngine
   614     // reconnect every hour to force deallocate websocket garbage
   615     // reconnect every hour to force deallocate websocket garbage
   615     if(window.navigator.userAgent.includes("QtWebEngine")){
   616     if(window.navigator.userAgent.includes("QtWebEngine")){
   616         if(periodic_reconnect_timer){
   617         if(periodic_reconnect_timer){
   617             window.clearTimeout(periodic_reconnect_timer);
   618             window.clearTimeout(periodic_reconnect_timer);
   618         }
   619         }
   619         periodic_reconnect_timer = window.setTimeout(() => {
   620         periodic_reconnect_timer = window.setTimeout(() => {
       
   621             force_reconnect = true;
   620             ws.close();
   622             ws.close();
   621             periodic_reconnect_timer = null;
   623             periodic_reconnect_timer = null;
   622         }, 3600000);
   624         }, 3600000);
   623     }
   625     }
   624 
   626 
   633 };
   635 };
   634 
   636 
   635 function ws_onclose(evt) {
   637 function ws_onclose(evt) {
   636     console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in "+reconnect_delay+"ms.");
   638     console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in "+reconnect_delay+"ms.");
   637     ws = null;
   639     ws = null;
   638     // reconect
   640     // Do not attempt to reconnect immediately in case:
   639     // TODO : add visible notification while waiting for reload
   641     //    - connection was closed by server (PLC stop)
   640     console.log(evt.wasClean)
   642     //    - connection was closed locally with an intention to reconnect
   641     console.log(evt.reason)
   643     if(evt.code=1000 && !force_reconnect){
   642     if(evt.code=1000){
       
   643         // Do not attempt to reconnect immediately in case of Normal Closure
       
   644         window.alert("Connection closed by server");
   644         window.alert("Connection closed by server");
   645         location.reload();
   645         location.reload();
   646     }
   646     }
   647     window.setTimeout(create_ws, reconnect_delay);
   647     window.setTimeout(create_ws, reconnect_delay);
   648     reconnect_delay += 500;
   648     reconnect_delay += 500;
       
   649     force_reconnect = false;
   649 };
   650 };
   650 
   651 
   651 var ws_url =
   652 var ws_url =
   652     window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws')
   653     window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws')
   653     + '?mode=' + (has_watchdog ? "watchdog" : "multiclient");
   654     + '?mode=' + (has_watchdog ? "watchdog" : "multiclient");