aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Module/Sse.php
diff options
context:
space:
mode:
Diffstat (limited to 'Zotlabs/Module/Sse.php')
-rw-r--r--Zotlabs/Module/Sse.php97
1 files changed, 58 insertions, 39 deletions
diff --git a/Zotlabs/Module/Sse.php b/Zotlabs/Module/Sse.php
index 8b46dcafd..673457db1 100644
--- a/Zotlabs/Module/Sse.php
+++ b/Zotlabs/Module/Sse.php
@@ -4,6 +4,7 @@ namespace Zotlabs\Module;
use App;
use Zotlabs\Lib\Apps;
+use Zotlabs\Lib\Config;
use Zotlabs\Web\Controller;
use Zotlabs\Lib\Enotify;
use Zotlabs\Lib\XConfig;
@@ -18,25 +19,20 @@ class Sse extends Controller {
function init() {
+ // This is important!
+ session_write_close();
+ ignore_user_abort(true);
+
if((observer_prohibited(true))) {
killme();
}
- if(! intval(get_config('system','open_pubstream',1))) {
- if(! get_observer_hash()) {
- killme();
- }
- }
-
- // this is important!
- session_write_close();
-
self::$uid = local_channel();
self::$ob_hash = get_observer_hash();
self::$sse_id = false;
self::$vnotify = -1;
- if(! self::$ob_hash) {
+ if (!self::$ob_hash) {
if(session_id()) {
self::$sse_id = true;
self::$ob_hash = 'sse_id.' . session_id();
@@ -52,9 +48,9 @@ class Sse extends Controller {
$sleep = 1000000; // microseconds
- self::$sse_enabled = get_config('system', 'sse_enabled', 0);
+ self::$sse_enabled = Config::Get('system', 'sse_enabled', 0);
- if(self::$sse_enabled) {
+ if (self::$sse_enabled) {
// Server Sent Events
@@ -72,7 +68,7 @@ class Sse extends Controller {
$i = 0;
}
- if(!self::$sse_id && $i === 0) {
+ if (!self::$sse_id && $i === 0) {
// Update chat presence indication about once per minute
$r = q("select cp_id, cp_room from chatpresence where cp_xchan = '%s' and cp_client = '%s' and cp_room = 0 limit 1",
dbesc(self::$ob_hash),
@@ -81,7 +77,7 @@ class Sse extends Controller {
$basic_presence = false;
- if($r) {
+ if ($r) {
$basic_presence = true;
q("update chatpresence set cp_last = '%s' where cp_id = %d",
dbesc(datetime_convert()),
@@ -89,7 +85,7 @@ class Sse extends Controller {
);
}
- if(!$basic_presence) {
+ if (!$basic_presence) {
q("insert into chatpresence ( cp_xchan, cp_last, cp_status, cp_client)
values( '%s', '%s', '%s', '%s' ) ",
dbesc(self::$ob_hash),
@@ -100,16 +96,16 @@ class Sse extends Controller {
}
}
+ $result = [];
+
XConfig::Load(self::$ob_hash);
- $result = [];
$lock = XConfig::Get(self::$ob_hash, 'sse', 'lock');
if (!$lock) {
$result = XConfig::Get(self::$ob_hash, 'sse', 'notifications', []);
}
-
// We do not have the local_channel in the addon.
// Reset pubs here if the app is not installed.
if (self::$uid && (!(self::$vnotify & VNOTIFY_PUBS) || !Apps::system_app_installed(self::$uid, 'Public Stream'))) {
@@ -118,42 +114,61 @@ class Sse extends Controller {
}
}
- if($result) {
+ if ($result) {
echo "event: notifications\n";
echo 'data: ' . json_encode($result);
echo "\n\n";
-
- XConfig::Set(self::$ob_hash, 'sse', 'notifications', []);
- unset($result);
+ }
+ else {
+ // if no result we will send a heartbeat to keep connected
+ echo "event: heartbeat\n";
+ echo 'data: {}';
+ echo "\n\n";
}
- // always send heartbeat to detect disconnected clients
- echo "event: heartbeat\n";
- echo 'data: {}';
- echo "\n\n";
+ if (connection_status() != CONNECTION_NORMAL || connection_aborted()) {
- if(ob_get_length() > 0)
- ob_end_flush();
+ // In case session_write_close() failed for some reason and
+ // the channel was changed we might need to reset the
+ // session to it's current stored state here.
+ // Otherwise the uid might switch back to the previous value
+ // in the background.
- flush();
+ session_reset();
- if(connection_status() != CONNECTION_NORMAL || connection_aborted()) {
- //TODO: this does not seem to be triggered
XConfig::Set(self::$ob_hash, 'sse', 'timestamp', NULL_DATE);
- break;
+ XConfig::Set(self::$ob_hash, 'sse', 'notifications', []);
+
+ if (ob_get_length() > 0) {
+ ob_end_flush();
+ }
+
+ flush();
+
+ exit;
}
- $i++;
+ if (ob_get_length() > 0) {
+ ob_flush();
+ }
+
+ flush();
usleep($sleep);
+ if ($result) {
+ XConfig::Set(self::$ob_hash, 'sse', 'notifications', []);
+ }
+
+ $i++;
+
}
}
else {
// Fallback to traditional polling
- if(! self::$sse_id) {
+ if(!self::$sse_id) {
// Update chat presence indication
@@ -162,14 +177,14 @@ class Sse extends Controller {
dbesc($_SERVER['REMOTE_ADDR'])
);
$basic_presence = false;
- if($r) {
+ if ($r) {
$basic_presence = true;
q("update chatpresence set cp_last = '%s' where cp_id = %d",
dbesc(datetime_convert()),
intval($r[0]['cp_id'])
);
}
- if(! $basic_presence) {
+ if (!$basic_presence) {
q("insert into chatpresence ( cp_xchan, cp_last, cp_status, cp_client)
values( '%s', '%s', '%s', '%s' ) ",
dbesc(self::$ob_hash),
@@ -180,17 +195,21 @@ class Sse extends Controller {
}
}
+ $result = [];
+
XConfig::Load(self::$ob_hash);
- $result = XConfig::Get(self::$ob_hash, 'sse', 'notifications', []);
$lock = XConfig::Get(self::$ob_hash, 'sse', 'lock');
- if($result && !$lock) {
+ if (!$lock) {
+ $result = XConfig::Get(self::$ob_hash, 'sse', 'notifications', []);
+ }
+
+ if ($result) {
XConfig::Set(self::$ob_hash, 'sse', 'notifications', []);
- json_return_and_die($result);
}
- killme();
+ json_return_and_die($result);
}