# HG changeset patch # User Goffi # Date 1680188445 -7200 # Node ID ff67c700405ed1dcb5adde358b4580622bfeadf0 # Parent cac1a96f3df8e57b0af60b0c7cc941c0c82eda28 remove websocket legacy code: It is not needed anymore as websocket are not handled with Brython diff -r cac1a96f3df8 -r ff67c700405e sat_templates/templates/bulma/base/base.html --- a/sat_templates/templates/bulma/base/base.html Fri Nov 25 17:07:26 2022 +0100 +++ b/sat_templates/templates/bulma/base/base.html Thu Mar 30 17:00:45 2023 +0200 @@ -70,9 +70,8 @@ {% endif %} - {# FIXME: websocket handling is to be replaced by Brython #} {% if websocket is defined %} - {{ script.include('websocket', '') }} + {% endif %} {{ script.generate_scripts() }} @@ -81,11 +80,6 @@ {{script.content|safe}} {% endfor %} - {# FIXME: websocket handling is to be replaced by Brython #} - {% if websocket is defined %} - - {% endif %} - {% block favicon %} {% endblock favicon %} diff -r cac1a96f3df8 -r ff67c700405e sat_templates/templates/default/static/websocket.js --- a/sat_templates/templates/default/static/websocket.js Fri Nov 25 17:07:26 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -/* -SàT templates: suit of templates for Salut à Toi -Copyright (C) 2017 Jérôme Poisson (goffi@goffi.org) - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU Affero General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Affero General Public License for more details. - -You should have received a copy of the GNU Affero General Public License -along with this program. If not, see . -*/ - -/* websocket handler */ - - -//TODO: retry websocket instead of reload -function WSHandler(url, token, debug=false) { - var socket = new WebSocket(url, 'libervia_page_' + token ); - var retried = 0; - - var errorHandler = function(error) { - if (retried > 20) { - console.error("Too many tries, can't start websocket"); - alert("Dynamic connection with server can't be established, please try to reload this page in a while or contact your service administrator"); - return; - } - var delay = Math.floor((Math.random() * 10) + 1) + 30 * Math.min(retried, 6); - notifyRetry(delay, function() { - retried++; - socket = new WebSocket(url, 'libervia_page_' + token ); - socket.addEventListener('error', errorHandler); - }); - }; - - socket.addEventListener('error', errorHandler); - - if (debug) { - socket.addEventListener('message', function(event) { - console.log('WS in <== ', JSON.parse(event.data)); - }); - } - - socket.addEventListener('message', function(event) { - try {data = JSON.parse(event.data);} - catch (e) { - console.warn('invalid websocket message received: %s', e); - return; - } - switch (data.type) { - case 'reload': - location.reload(true); - break; - case 'dom': - selected_element = document.body.querySelector(data.selectors); - switch (data.update_type) { - case 'append': - var template = document.createElement('template'); - template.innerHTML = data.html.trim(); - new_element = template.content.firstChild; - selected_element.appendChild(new_element); - break; - default: - console.warn('Unknown DOM update type: %s', data.update_type); - } - break; - default: - console.warn('Unknown data type: %s', data.type); - } - }); - - socket.addEventListener('open', function (event) { - console.log('Websocket opened'); - retried = 0; - }.bind(this)); - - this.send = function(data) { - if (debug) { - console.log('WS out ==> ', data); - } - socket.send(JSON.stringify(data)); - }; - - function notifyRetry(timeout, retryCb) { - /* Show a notification dialog informing the user that server can't be reach - * and call retryCb after timeout seconds. - * A "retry now" link allows to retry immediately" - * - * @param timeout(int): delay before retrying, in seconds - * @param retryCb(function): function to call when retrying - */ - var startTime = Date.now() / 1000; - var retryIntervalID; - var notif = document.createElement("div"); - notif.setAttribute('class', 'notification retry'); - //FIXME: we use English without translation for now, must be changed when we can use gettext in Libervia pages - notif.innerHTML = "

Can't reach the server, retrying in seconds

retry now

"; - document.body.appendChild(notif); - var retryCounter = document.getElementById('retry_counter'); - retryCounter.textContent = timeout; - - var retry = function () { - clearInterval(retryIntervalID); - notif.parentNode.removeChild(notif); - retryCb(); - }; - - var updateTimer = function () { - var elapsed = Math.floor(Date.now() / 1000 - startTime); - var remaining = timeout - elapsed; - if (remaining < 0) { - retry(); - } - else { - retryCounter.textContent = remaining; - } - }; - - var retryNow = document.getElementById('retry_now'); - retryNow.addEventListener('click', function(){ - retry(); - }); - - retryIntervalID = setInterval(updateTimer, 1000); - } -}