--- a/svghmi/svghmi.js Tue Oct 18 11:09:40 2022 +0200
+++ b/svghmi/svghmi.js Fri Oct 21 10:39:43 2022 +0200
@@ -23,13 +23,6 @@
// Open WebSocket to relative "/ws" address
var has_watchdog = window.location.hash == "#watchdog";
- window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws')
- + '?mode=' + (has_watchdog ? "watchdog" : "multiclient");
-var ws = new WebSocket(ws_url);
-ws.binaryType = 'arraybuffer';
INT: (dv,offset) => [dv.getInt16(offset, true), 2],
BOOL: (dv,offset) => [dv.getInt8(offset, true), 1],
@@ -98,7 +91,7 @@
// Message reception handler
// Hash is verified and HMI values updates resulting from binary parsing
// are stored until browser can compute next frame, DOM is left untouched
-ws.onmessage = function (evt) {
+function ws_onmessage(evt) { let dv = new DataView(data);
@@ -140,8 +133,10 @@
hmi_hash_u8 = new Uint8Array(hmi_hash);
function send_blob(data) {
+ if(ws && data.length > 0) { ws.send(new Blob([hmi_hash_u8].concat(data)));
@@ -199,6 +194,11 @@
+function reset_subscription_periods() { + for(let index in subscriptions) + subscriptions[index][1] = 0; // artificially subscribe the watchdog widget to "/heartbeat" hmi variable
// Since dispatch directly calls change_hmi_value,
@@ -298,6 +298,10 @@
function update_subscriptions() {
+ // dont' change subscriptions if not connected for(let index in subscriptions){
let widgets = subscribers(index);
@@ -418,12 +422,13 @@
-function prepare_svg() {
- // prevents context menu from appearing on right click and long touch
- document.body.addEventListener('contextmenu', e => {
+// prevents context menu from appearing on right click and long touch +document.body.addEventListener('contextmenu', e => { +function detach_detachables() { for(let eltid in detachable_elements){
let [element,parent] = detachable_elements[eltid];
@@ -572,24 +577,65 @@
+apply_reference_frames(); +switch_page(default_page); +var reconnect_delay = 0; +// var periodic_reconnect_timer; // Once connection established
-ws.onopen = function (evt) {
- apply_reference_frames();
+function ws_onopen(evt) { + // to force reconnect every hour + if(periodic_reconnect_timer){ + window.clearTimeout(periodic_reconnect_timer); + periodic_reconnect_timer = window.setTimeout(() => { + periodic_reconnect_timer = null; + // forget subscriptions remotely
- switch_page(default_page);
+ // forget earlier subscriptions locally + reset_subscription_periods(); + // update PLC about subscriptions and current page + // at first try reconnect immediately -ws.onclose = function (evt) {
+function ws_onclose(evt) { + console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in "+reconnect_delay+"ms."); // TODO : add visible notification while waiting for reload
- console.log("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+" Reload in 10s.");
- // TODO : re-enable auto reload when not in debug
- //window.setTimeout(() => location.reload(true), 10000);
- alert("Connection closed. code:"+evt.code+" reason:"+evt.reason+" wasClean:"+evt.wasClean+".");
+ window.setTimeout(create_ws, reconnect_delay); + reconnect_delay += 500; + window.location.href.replace(/^http(s?:\/\/[^\/]*)\/.*$/, 'ws$1/ws') + + '?mode=' + (has_watchdog ? "watchdog" : "multiclient");
+ ws = new WebSocket(ws_url); + ws.binaryType = 'arraybuffer'; + ws.onmessage = ws_onmessage; + ws.onclose = ws_onclose; const xmlns = "http://www.w3.org/2000/svg";