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

View File

@ -255,7 +255,8 @@ abstract class Ajax extends Api\Framework
Api\Hooks::process(array(
'location' => 'framework_header',
'popup' => !$do_framework,
));
'extra' => &$extra,
), [], true);
$this->tpl->set_var($this->_get_header($extra));
$content = $this->tpl->fp('out','head').$content;

View File

@ -32,7 +32,7 @@ class ContentSecurityPolicy
private static $sources = array(
'script-src' => array("'unsafe-eval'"),
'style-src' => array("'unsafe-inline'"),
'connect-src' => array(),
'connect-src' => null, // NOT array(), to allow setting no default connect-src!
'frame-src' => null, // NOT array(), to allow setting no default frame-src!
);
@ -50,10 +50,11 @@ class ContentSecurityPolicy
if (!isset(self::$sources[$source]))
{
// set frame-src attrs of API and apps via hook
if ($source == 'frame-src' && !isset($attrs))
if (in_array($source, ['frame-src', 'connect-src']) && !isset($attrs))
{
$attrs = array('www.egroupware.org');
if (($app_additional = Api\Hooks::process('csp-frame-src')))
$attrs = [];
// no permssion / user-run-rights check for connect-src
if (($app_additional = Api\Hooks::process('csp-'.$source, [], $source === 'connect-src')))
{
foreach($app_additional as $addtional)
{
@ -135,6 +136,7 @@ class ContentSecurityPolicy
*/
public static function send()
{
self::add('connect-src', null); // set defaults for connect-src (no run rights checked)
self::add('frame-src', null); // set defaults for frame-src
$policies = array();

View File

@ -7,11 +7,12 @@
* @package api
* @subpackage json
* @author Ralf Becker <rb@stylite.de>
* @version $Id$
*/
namespace EGroupware\Api\Json;
use EGroupware\Api;
/**
* Class to push JSON commands to client
*/
@ -41,9 +42,10 @@ class Push extends Msg
/**
*
* @param int $account_id account_id of user to push to
* @param int $account_id =null account_id to push message too or
* null: for current session only or 0 for whole instance / broadcast
*/
public function __construct($account_id)
public function __construct($account_id=null)
{
$this->account_id = $account_id;
}
@ -59,6 +61,14 @@ class Push extends Msg
{
if (!isset(self::$backend))
{
// we prepend so the default backend stays last
foreach(Api\Hooks::process('push-backends', [], true) as $class)
{
if (!empty($class))
{
array_unshift(self::$backends, $class);
}
}
foreach(self::$backends as $class)
{
if (class_exists($class))
@ -69,7 +79,8 @@ class Push extends Msg
}
catch (\Exception $e) {
// ignore all exceptions
unset($e, self::$backend);
unset($e);
self::$backend = null;
}
}
}

View File

@ -20,7 +20,8 @@ interface PushBackend
/**
* Adds any type of data to the message
*
* @param int $account_id account_id to push message too
* @param int $account_id =null account_id to push message too or
* null: for current session only or 0 for whole instance / broadcast
* @param string $key
* @param mixed $data
* @throws Exception\NotOnline if $account_id is not online

View File

@ -171,6 +171,11 @@ class Response extends Msg
*/
protected function addGeneric($key, $data)
{
/* send testwise all message responses via push server
if ($key === 'message' || $key === 'apply' && $data['func'] === 'egw.message')
{
return (new Push())->addGeneric($key, $data);
}*/
self::get()->responseArray[] = array(
'type' => $key,
'data' => $data,

View File

@ -78,7 +78,8 @@ class notifications_push implements Json\PushBackend
/**
* Adds any type of data to the message
*
* @param int $account_id account_id to push message too
* @param int|null $account_id account_id to push message too, 0 for broadcast
* or null for curent session (we can only send to current user)
* @param string $key
* @param mixed $data
*
@ -88,6 +89,8 @@ class notifications_push implements Json\PushBackend
*/
public function addGeneric($account_id, $key, $data)
{
if (!isset($account_id)) $account_id = $GLOBALS['egw_info']['user']['account_id'];
self::$db->insert(self::TABLE, array(
'account_id' => $account_id,
'notify_type' => self::TYPE,