diff --git a/core/js/eventsource.js b/core/js/eventsource.js new file mode 100644 index 0000000000..422f97657b --- /dev/null +++ b/core/js/eventsource.js @@ -0,0 +1,94 @@ +/** + * 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 . + * + */ + +/** + * 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 + */ + +OC.EventSource=function(src){ + if(!this.useFallBack && typeof EventSource !='undefined'){ + this.source=new EventSource(src); + this.source.onmessage=function(e){ + for(var i=0;i'); + this.iframe.attr('id',iframeId); + this.iframe.hide(); + this.iframe.attr('src',src+'?fallback=true&fallback_id='+OC.EventSource.iframeCount); + $('body').append(this.iframe); + this.useFallBack=true; + OC.EventSource.iframeCount++ + + } +} +OC.EventSource.fallBackSources=[]; +OC.EventSource.iframeCount=0;//number of fallback iframes +OC.EventSource.fallBackCallBack=function(id,type,data){ + OC.EventSource.fallBackSources[id].fallBackCallBack(type,JSON.parse(data)); +} +OC.EventSource.prototype={ + typelessListeners:[], + iframe:null, + listeners:{},//only for fallback + useFallBack:false, + fallBackCallBack:function(type,data){ + if(type){ + for(var i=0;i. +* +*/ + +/** + * 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