forked from DGNum/gestioCOF
Merge branch 'Aufinal/websockets' into 'master'
K-Fêt : - Ajout d'une classe générale pour les websockets See merge request !204
This commit is contained in:
commit
fafa7e536e
2 changed files with 49 additions and 11 deletions
|
@ -27,6 +27,53 @@ $(document).ready(function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generic Websocket class and k-psul ws instanciation
|
||||||
|
*/
|
||||||
|
|
||||||
|
class KfetWebsocket {
|
||||||
|
|
||||||
|
static get defaults() {
|
||||||
|
return {"relative_url": "", "default_msg": {}, "handlers": []};
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(data) {
|
||||||
|
$.extend(this, this.constructor.defaults, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
get url() {
|
||||||
|
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;
|
||||||
|
|
||||||
|
return websocket_protocol+"://" + location_url + this.relative_url ;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_handler(handler) {
|
||||||
|
if (!this.socket)
|
||||||
|
this.listen();
|
||||||
|
|
||||||
|
this.handlers.push(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
listen() {
|
||||||
|
var that = this;
|
||||||
|
this.socket = new ReconnectingWebSocket(this.url);
|
||||||
|
|
||||||
|
this.socket.onmessage = function(e) {
|
||||||
|
var data = $.extend({}, that.default_msg, JSON.parse(e.data));
|
||||||
|
for (let handler of that.handlers) {
|
||||||
|
handler(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var OperationWebSocket = new KfetWebsocket({
|
||||||
|
'relative_url': '/ws/k-fet/k-psul/',
|
||||||
|
'default_msg': {'opegroups':[],'opes':[],'checkouts':[],'articles':[]},
|
||||||
|
});
|
||||||
|
|
||||||
function dateUTCToParis(date) {
|
function dateUTCToParis(date) {
|
||||||
return moment.tz(date, 'UTC').tz('Europe/Paris');
|
return moment.tz(date, 'UTC').tz('Europe/Paris');
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
<script type="text/javascript" src="{% static 'kfet/js/moment.js' %}"></script>
|
<script type="text/javascript" src="{% static 'kfet/js/moment.js' %}"></script>
|
||||||
<script type="text/javascript" src="{% static 'kfet/js/moment-fr.js' %}"></script>
|
<script type="text/javascript" src="{% static 'kfet/js/moment-fr.js' %}"></script>
|
||||||
<script type="text/javascript" src="{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}"></script>
|
<script type="text/javascript" src="{% static 'kfet/js/moment-timezone-with-data-2010-2020.js' %}"></script>
|
||||||
<script type="text/javascript" src="{% static 'kfet/js/kfet.js' %}"></script>
|
|
||||||
<script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script>
|
<script type="text/javascript" src="{% static 'kfet/js/history.js' %}"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
@ -1321,15 +1320,7 @@ $(document).ready(function() {
|
||||||
// Synchronization
|
// Synchronization
|
||||||
// -----
|
// -----
|
||||||
|
|
||||||
websocket_msg_default = {'opegroups':[],'opes':[],'checkouts':[],'articles':[]}
|
OperationWebSocket.add_handler(function(data) {
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
for (var i=0; i<data['opegroups'].length; i++) {
|
for (var i=0; i<data['opegroups'].length; i++) {
|
||||||
if (data['opegroups'][i]['add']) {
|
if (data['opegroups'][i]['add']) {
|
||||||
khistory.addOpeGroup(data['opegroups'][i]);
|
khistory.addOpeGroup(data['opegroups'][i]);
|
||||||
|
@ -1360,7 +1351,7 @@ $(document).ready(function() {
|
||||||
settings['addcost_amount'] = parseFloat(data['addcost']['amount']);
|
settings['addcost_amount'] = parseFloat(data['addcost']['amount']);
|
||||||
displayAddcost();
|
displayAddcost();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
// General
|
// General
|
||||||
|
|
Loading…
Reference in a new issue