From 45038af948cd07ffc74efc8d4b0282fa11b7de7e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 30 Jan 2012 20:19:51 +0100 Subject: provide small wrapper around server side events and provide a fallback for IE --- lib/base.php | 1 + lib/eventsource.php | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 lib/eventsource.php (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index ab7cb2990ed..4da17c70a57 100644 --- a/lib/base.php +++ b/lib/base.php @@ -187,6 +187,7 @@ class OC{ OC_Util::addScript( "jquery.infieldlabel.min" ); OC_Util::addScript( "jquery-tipsy" ); OC_Util::addScript( "js" ); + OC_Util::addScript( "eventsource" ); //OC_Util::addScript( "multiselect" ); OC_Util::addScript('search','result'); OC_Util::addStyle( "styles" ); diff --git a/lib/eventsource.php b/lib/eventsource.php new file mode 100644 index 00000000000..c123eb4b837 --- /dev/null +++ b/lib/eventsource.php @@ -0,0 +1,73 @@ +. +* +*/ + +/** + * wrapper for server side events (http://en.wikipedia.org/wiki/Server-sent_events) + * includes a fallback for older browsers and IE + * + * use server side events with causion, to many open requests can hang the server + */ +class OC_EventSource{ + private $fallback; + private $fallBackId=0; + + public function __construct(){ + ob_end_clean(); + header('Cache-Control: no-cache'); + $this->fallback=isset($_GET['fallback']) and $_GET['fallback']=='true'; + if($this->fallback){ + $fallBackId=$_GET['fallback_id']; + header("Content-Type: text/html"); + echo str_repeat(''.PHP_EOL,10); //dummy data to keep IE happy + }else{ + header("Content-Type: text/event-stream"); + } + flush(); + + } + + /** + * send a message to the client + * @param string type + * @param object data + * + * if only one paramater is given, a typeless message will be send with that paramater as data + */ + public function send($type,$data=null){ + if(is_null($data)){ + $data=$type; + $type=null; + } + if($this->fallback){ + $response=''.PHP_EOL; + echo $response; + }else{ + if($type){ + echo 'event: '.$type.PHP_EOL; + } + echo 'data: '.json_encode($data).PHP_EOL; + } + echo PHP_EOL; + flush(); + } +} \ No newline at end of file -- cgit v1.2.3