eventsource.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_requesttoken;
  42. if(!this.useFallBack && typeof EventSource !='undefined'){
  43. var joinChar = '&';
  44. if(src.indexOf('?') == -1) {
  45. joinChar = '?';
  46. }
  47. this.source=new EventSource(src+joinChar+dataStr);
  48. this.source.onmessage=function(e){
  49. for(var i=0;i<this.typelessListeners.length;i++){
  50. this.typelessListeners[i](JSON.parse(e.data));
  51. }
  52. }.bind(this);
  53. }else{
  54. iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount;
  55. OC.EventSource.fallBackSources[OC.EventSource.iframeCount]=this;
  56. this.iframe=$('<iframe/>');
  57. this.iframe.attr('id',iframeId);
  58. this.iframe.hide();
  59. var joinChar = '&';
  60. if(src.indexOf('?') == -1) {
  61. joinChar = '?';
  62. }
  63. this.iframe.attr('src',src+joinChar+'fallback=true&fallback_id='+OC.EventSource.iframeCount+'&'+dataStr);
  64. $('body').append(this.iframe);
  65. this.useFallBack=true;
  66. OC.EventSource.iframeCount++
  67. }
  68. //add close listener
  69. this.listen('__internal__',function(data){
  70. if(data=='close'){
  71. this.close();
  72. }
  73. }.bind(this));
  74. }
  75. OC.EventSource.fallBackSources=[];
  76. OC.EventSource.iframeCount=0;//number of fallback iframes
  77. OC.EventSource.fallBackCallBack=function(id,type,data){
  78. OC.EventSource.fallBackSources[id].fallBackCallBack(type,data);
  79. }
  80. OC.EventSource.prototype={
  81. typelessListeners:[],
  82. iframe:null,
  83. listeners:{},//only for fallback
  84. useFallBack:false,
  85. fallBackCallBack:function(type,data){
  86. if(type){
  87. if (typeof this.listeners['done'] != 'undefined') {
  88. for(var i=0;i<this.listeners[type].length;i++){
  89. this.listeners[type][i](data);
  90. }
  91. }
  92. }else{
  93. for(var i=0;i<this.typelessListeners.length;i++){
  94. this.typelessListeners[i](data);
  95. }
  96. }
  97. },
  98. lastLength:0,//for fallback
  99. listen:function(type,callback){
  100. if(callback && callback.call){
  101. if(type){
  102. if(this.useFallBack){
  103. if(!this.listeners[type]){
  104. this.listeners[type]=[];
  105. }
  106. this.listeners[type].push(callback);
  107. }else{
  108. this.source.addEventListener(type,function(e){
  109. callback(JSON.parse(e.data));
  110. },false);
  111. }
  112. }else{
  113. typelessListeners.push(callback);
  114. }
  115. }
  116. },
  117. close:function(){
  118. if (typeof this.source !='undefined') {
  119. this.source.close();
  120. }
  121. }
  122. }