Websocket class and instance

This commit is contained in:
Ludovic Stephan 2017-03-31 12:23:27 -03:00
parent 63cb7b7fd0
commit 499c081565
2 changed files with 35 additions and 11 deletions

View file

@ -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) { function dateUTCToParis(date) {
return moment.tz(date, 'UTC').tz('Europe/Paris'); return moment.tz(date, 'UTC').tz('Europe/Paris');
} }

View file

@ -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.listen(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