eventsource.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * ownCloud
  3. *
  4. * @author Robin Appelman
  5. * @copyright 2012 Robin Appelman icewind1991@gmail.com
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /**
  22. * wrapper for server side events (http://en.wikipedia.org/wiki/Server-sent_events)
  23. * includes a fallback for older browsers and IE
  24. *
  25. * use server side events with causion, to many open requests can hang the server
  26. */
  27. /**
  28. * create a new event source
  29. * @param string src
  30. * @param object data to be send as GET
  31. */
  32. OC.EventSource=function(src,data){
  33. var dataStr='';
  34. this.typelessListeners=[];
  35. this.listeners={};
  36. if(data){
  37. for(name in data){
  38. dataStr+=name+'='+encodeURIComponent(data[name])+'&';
  39. }
  40. }
  41. dataStr+='requesttoken='+OC.Request.Token;
  42. if(!this.useFallBack && typeof EventSource !='undefined'){
  43. this.source=new EventSource(src+'?'+dataStr);
  44. this.source.onmessage=function(e){
  45. for(var i=0;i<this.typelessListeners.length;i++){
  46. this.typelessListeners[i](JSON.parse(e.data));
  47. }
  48. }.bind(this);
  49. }else{
  50. iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount;
  51. OC.EventSource.fallBackSources[OC.EventSource.iframeCount]=this;
  52. this.iframe=$('<iframe/>');
  53. this.iframe.attr('id',iframeId);
  54. this.iframe.hide();
  55. this.iframe.attr('src',src+'?fallback=true&fallback_id='+OC.EventSource.iframeCount+'&'+dataStr);
  56. $('body').append(this.iframe);
  57. this.useFallBack=true;
  58. OC.EventSource.iframeCount++
  59. }
  60. //add close listener
  61. this.listen('__internal__',function(data){
  62. if(data=='close'){
  63. this.close();
  64. }
  65. }.bind(this));
  66. }
  67. OC.EventSource.fallBackSources=[];
  68. OC.EventSource.iframeCount=0;//number of fallback iframes
  69. OC.EventSource.fallBackCallBack=function(id,type,data){
  70. OC.EventSource.fallBackSources[id].fallBackCallBack(type,data);
  71. }
  72. OC.EventSource.prototype={
  73. typelessListeners:[],
  74. iframe:null,
  75. listeners:{},//only for fallback
  76. useFallBack:false,
  77. fallBackCallBack:function(type,data){
  78. if(type){
  79. for(var i=0;i<this.listeners[type].length;i++){
  80. this.listeners[type][i](data);
  81. }
  82. }else{
  83. for(var i=0;i<this.typelessListeners.length;i++){
  84. this.typelessListeners[i](data);
  85. }
  86. }
  87. },
  88. lastLength:0,//for fallback
  89. listen:function(type,callback){
  90. if(callback && callback.call){
  91. if(type){
  92. if(this.useFallBack){
  93. if(!this.listeners[type]){
  94. this.listeners[type]=[];
  95. }
  96. this.listeners[type].push(callback);
  97. }else{
  98. this.source.addEventListener(type,function(e){
  99. callback(JSON.parse(e.data));
  100. },false);
  101. }
  102. }else{
  103. typelessListeners.push(callback);
  104. }
  105. }
  106. },
  107. close:function(){
  108. this.source.close();
  109. }
  110. }