From 499c08156577f2e94894d8e9dbfab242d42062c1 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Fri, 31 Mar 2017 12:23:27 -0300 Subject: [PATCH] Websocket class and instance --- kfet/static/kfet/js/kfet.js | 33 +++++++++++++++++++++++++++++++++ kfet/templates/kfet/kpsul.html | 13 ++----------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/kfet/static/kfet/js/kfet.js b/kfet/static/kfet/js/kfet.js index f0e7a316..aa4b7739 100644 --- a/kfet/static/kfet/js/kfet.js +++ b/kfet/static/kfet/js/kfet.js @@ -27,6 +27,39 @@ $(document).ready(function() { } }); +/* + * Generic Websocket class and k-psul ws instanciation + */ + +class Websocket { + + static get default_data() { + return {"relative_url": "", "default_msg": {}}; + } + + constructor(data) { + $.extend(this, this.constructor.default_data, data); + } + + listen(handle_func) { + var that = this; + var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws'; + var location_host = window.location.host; + var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; + var socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + this.relative_url); + + socket.onmessage = function(e) { + var data = $.extend({}, that.default_msg, JSON.parse(e.data)); + handle_func(data); + } + } +} + +var OperationWebSocket = new Websocket({ + 'relative_url': '/ws/k-fet/k-psul/', + 'default_msg': {'opegroups':[],'opes':[],'checkouts':[],'articles':[]}, +}); + function dateUTCToParis(date) { return moment.tz(date, 'UTC').tz('Europe/Paris'); } diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index d3b148c9..cf558647 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -12,7 +12,6 @@ - {% endblock %} @@ -1321,15 +1320,7 @@ $(document).ready(function() { // Synchronization // ----- - websocket_msg_default = {'opegroups':[],'opes':[],'checkouts':[],'articles':[]} - - var websocket_protocol = window.location.protocol == 'https:' ? 'wss' : 'ws'; - var location_host = window.location.host; - var location_url = window.location.pathname.startsWith('/gestion/') ? location_host + '/gestion' : location_host; - socket = new ReconnectingWebSocket(websocket_protocol+"://" + location_url + "/ws/k-fet/k-psul/"); - socket.onmessage = function(e) { - data = $.extend({}, websocket_msg_default, JSON.parse(e.data)); - + OperationWebSocket.listen(function(data) { for (var i=0; i