mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
[fix] Connect to WebSocket route before every API call
This commit is contained in:
parent
9215420093
commit
3fc7599a99
1 changed files with 142 additions and 153 deletions
33
js/app.js
33
js/app.js
|
@ -93,31 +93,23 @@ app = Sammy('#main', function (sam) {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// Websocket connection
|
// API call
|
||||||
websocket: function(callback) {
|
api: function(uri, callback, method, data) {
|
||||||
c = this;
|
c = this;
|
||||||
if (store.get('connected')) {
|
|
||||||
c.ws = new WebSocket('wss://'+ store.get('url') +'/messages');
|
// Open a WebSocket connection to retrieve live messages from the moulinette
|
||||||
c.ws.onmessage = function(evt) {
|
ws = new WebSocket('wss://'+ store.get('url') +'/messages');
|
||||||
|
ws.onmessage = function(evt) {
|
||||||
console.log(evt.data);
|
console.log(evt.data);
|
||||||
$.each($.parseJSON(evt.data), function(k, v) {
|
$.each($.parseJSON(evt.data), function(k, v) {
|
||||||
c.flash(k, v);
|
c.flash(k, v);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
c.ws.onopen = function() {
|
|
||||||
if (typeof callback === 'function') {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c.ws.onclose = function() {
|
|
||||||
delete c.ws;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// API connection helper
|
// If not connected, WebSocket connection will raise an error, but we do not want to interrupt API request
|
||||||
api: function (uri, callback, method, data) {
|
ws.onerror = ws.onopen;
|
||||||
c = this;
|
|
||||||
|
ws.onopen = function(evt) {
|
||||||
method = typeof method !== 'undefined' ? method : 'GET';
|
method = typeof method !== 'undefined' ? method : 'GET';
|
||||||
data = typeof data !== 'undefined' ? data : {};
|
data = typeof data !== 'undefined' ? data : {};
|
||||||
if (window.navigator && window.navigator.language && (typeof data.locale === 'undefined')) {
|
if (window.navigator && window.navigator.language && (typeof data.locale === 'undefined')) {
|
||||||
|
@ -247,6 +239,7 @@ app = Sammy('#main', function (sam) {
|
||||||
store.clear('slide');
|
store.clear('slide');
|
||||||
c.redirect(store.get('path-1'));
|
c.redirect(store.get('path-1'));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Render view (cross-browser)
|
// Render view (cross-browser)
|
||||||
|
@ -396,7 +389,6 @@ app = Sammy('#main', function (sam) {
|
||||||
}
|
}
|
||||||
c.api('/login', function(data) {
|
c.api('/login', function(data) {
|
||||||
store.set('connected', true);
|
store.set('connected', true);
|
||||||
c.websocket();
|
|
||||||
|
|
||||||
$('.logout-button').fadeIn();
|
$('.logout-button').fadeIn();
|
||||||
c.flash('success', y18n.t('logged_in'));
|
c.flash('success', y18n.t('logged_in'));
|
||||||
|
@ -1079,9 +1071,6 @@ app = Sammy('#main', function (sam) {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
sam.websocket();
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue