Initialize an event source when we start using it, not in the constructor

This commit is contained in:
Robin Appelman 2014-08-29 17:18:11 +02:00
parent 6cdb1d89ae
commit 54c918fe48
1 changed files with 46 additions and 42 deletions

View File

@ -1,24 +1,9 @@
<?php
/**
* ownCloud
*
* @author Robin Appelman
* @copyright 2012 Robin Appelman icewind1991@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
/**
@ -28,10 +13,28 @@
* use server side events with caution, to many open requests can hang the server
*/
class OC_EventSource {
/**
* @var bool
*/
private $fallback;
/**
* @var int
*/
private $fallBackId = 0;
public function __construct() {
/**
* @var bool
*/
private $started = false;
protected function init() {
if ($this->started) {
return;
}
$this->started = true;
// prevent php output buffering, caching and nginx buffering
OC_Util::obEnd();
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');
@ -49,17 +52,18 @@ class OC_EventSource{
exit();
}
flush();
}
/**
* send a message to the client
*
* @param string $type
* @param mixed $data
*
* if only one parameter is given, a typeless message will be send with that parameter as data
*/
public function send($type, $data = null) {
$this->init();
if (is_null($data)) {
$data = $type;
$type = null;
@ -80,7 +84,7 @@ class OC_EventSource{
}
/**
* close the connection of the even source
* close the connection of the event source
*/
public function close() {
$this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it