support for new Swoole push server

This commit is contained in:
Ralf Becker
2019-11-04 09:29:49 +01:00
parent 9fd4ba1577
commit bf844b7598
8 changed files with 102 additions and 12 deletions

View File

@ -382,7 +382,7 @@
// Open tutorial popup with an introduction video about egroupware
if (window.framework === window.top.framework && typeof et2_dialog != 'undefined' &&
!egw.preference('egw_tutorial_noautoload', 'common') &&
!parseInt(document.getElementById('egw_script_id').getAttribute('data-framework-reload')) &&
!parseInt(egw_script.getAttribute('data-framework-reload')) &&
(!egw.config('egw_tutorial_disable', 'phpgwapi') || egw.config('egw_tutorial_disable', 'phpgwapi') == 'sidebox'))
{
// we need to wait until common translations are loaded
@ -409,6 +409,15 @@
{}, buttons, et2_dialog.QUESTION_MESSAGE, undefined, egw(window));
}, this);
}
// open websocket to push server for our top window
if (egw === window.top.egw && egw_script.getAttribute('data-websocket-url'))
{
egw.json('websocket', {}, undefined, this).openWebSocket(
egw_script.getAttribute('data-websocket-url'),
JSON.parse(egw_script.getAttribute('data-websocket-tokens'))
);
}
}
catch(e) {
// ignore SecurityError exception if top is different security context / cross-origin

View File

@ -94,6 +94,64 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
};
}
/**
* Open websocket to push server (and keeps it open)
*
* @param {string} url this.websocket(s)://host:port
* @param {array} tokens tokens to subscribe too
* @param {function} error option error callback(_msg) used instead our default this.error
* @param {int} reconnect timeout in ms (internal)
*/
json_request.prototype.openWebSocket = function(url, tokens, error, reconnect)
{
const min_reconnect_time = 1000;
const max_reconnect_time = 300000;
let reconnect_time = reconnect || min_reconnect_time;
this.websocket = new WebSocket(url);
this.websocket.onopen = jQuery.proxy(function(e)
{
reconnect_time = min_reconnect_time;
this.websocket.send(JSON.stringify({
subscribe: tokens
}));
}, this);
this.websocket.onmessage = jQuery.proxy(function(event)
{
console.log(event);
let data = JSON.parse(event.data);
if (data && data.type)
{
this.handleResponse({ response: [data]});
}
}, this);
this.websocket.onerror = jQuery.proxy(function(error)
{
console.log(error);
(error||this.handleError({}, error));
}, this);
this.websocket.onclose = jQuery.proxy(function(event)
{
if (event.wasClean)
{
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
}
else
{
reconnect_time *= 2;
if (reconnect_time > max_reconnect_time) reconnect_time = max_reconnect_time;
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.log('[close] Connection died --> reconnect in '+reconnect_time+'ms');
window.setTimeout(jQuery.proxy(this.openWebSocket, this, url, tokens, error, reconnect_time), reconnect_time);
}
}, this);
},
/**
* Sends the assembled request to the server
* @param {boolean} [async=false] Overrides async provided in constructor to give an easy way to make simple async requests