|
|
@ -28,6 +28,8 @@ |
|
|
|
* server |
|
|
|
* server |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* global EventSource */ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new event source |
|
|
|
* Create a new event source |
|
|
|
* @param {string} src |
|
|
|
* @param {string} src |
|
|
@ -35,7 +37,10 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
OC.EventSource=function(src,data){ |
|
|
|
OC.EventSource=function(src,data){ |
|
|
|
var dataStr=''; |
|
|
|
var dataStr=''; |
|
|
|
|
|
|
|
var name; |
|
|
|
|
|
|
|
var joinChar; |
|
|
|
this.typelessListeners=[]; |
|
|
|
this.typelessListeners=[]; |
|
|
|
|
|
|
|
this.closed = false; |
|
|
|
this.listeners={}; |
|
|
|
this.listeners={}; |
|
|
|
if(data){ |
|
|
|
if(data){ |
|
|
|
for(name in data){ |
|
|
|
for(name in data){ |
|
|
@ -43,8 +48,8 @@ OC.EventSource=function(src,data){ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
dataStr+='requesttoken='+oc_requesttoken; |
|
|
|
dataStr+='requesttoken='+oc_requesttoken; |
|
|
|
if(!this.useFallBack && typeof EventSource !='undefined'){ |
|
|
|
if(!this.useFallBack && typeof EventSource !== 'undefined'){ |
|
|
|
var joinChar = '&'; |
|
|
|
joinChar = '&'; |
|
|
|
if(src.indexOf('?') === -1) { |
|
|
|
if(src.indexOf('?') === -1) { |
|
|
|
joinChar = '?'; |
|
|
|
joinChar = '?'; |
|
|
|
} |
|
|
|
} |
|
|
@ -55,13 +60,13 @@ OC.EventSource=function(src,data){ |
|
|
|
} |
|
|
|
} |
|
|
|
}.bind(this); |
|
|
|
}.bind(this); |
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount; |
|
|
|
var iframeId='oc_eventsource_iframe_'+OC.EventSource.iframeCount; |
|
|
|
OC.EventSource.fallBackSources[OC.EventSource.iframeCount]=this; |
|
|
|
OC.EventSource.fallBackSources[OC.EventSource.iframeCount]=this; |
|
|
|
this.iframe=$('<iframe/>'); |
|
|
|
this.iframe=$('<iframe/>'); |
|
|
|
this.iframe.attr('id',iframeId); |
|
|
|
this.iframe.attr('id',iframeId); |
|
|
|
this.iframe.hide(); |
|
|
|
this.iframe.hide(); |
|
|
|
|
|
|
|
|
|
|
|
var joinChar = '&'; |
|
|
|
joinChar = '&'; |
|
|
|
if(src.indexOf('?') === -1) { |
|
|
|
if(src.indexOf('?') === -1) { |
|
|
|
joinChar = '?'; |
|
|
|
joinChar = '?'; |
|
|
|
} |
|
|
|
} |
|
|
@ -72,7 +77,7 @@ OC.EventSource=function(src,data){ |
|
|
|
} |
|
|
|
} |
|
|
|
//add close listener
|
|
|
|
//add close listener
|
|
|
|
this.listen('__internal__',function(data){ |
|
|
|
this.listen('__internal__',function(data){ |
|
|
|
if(data=='close'){ |
|
|
|
if(data === 'close'){ |
|
|
|
this.close(); |
|
|
|
this.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
}.bind(this)); |
|
|
|
}.bind(this)); |
|
|
@ -88,14 +93,19 @@ OC.EventSource.prototype={ |
|
|
|
listeners:{},//only for fallback
|
|
|
|
listeners:{},//only for fallback
|
|
|
|
useFallBack:false, |
|
|
|
useFallBack:false, |
|
|
|
fallBackCallBack:function(type,data){ |
|
|
|
fallBackCallBack:function(type,data){ |
|
|
|
|
|
|
|
var i; |
|
|
|
|
|
|
|
// ignore messages that might appear after closing
|
|
|
|
|
|
|
|
if (this.closed) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
if(type){ |
|
|
|
if(type){ |
|
|
|
if (typeof this.listeners['done'] != 'undefined') { |
|
|
|
if (typeof this.listeners.done !== 'undefined') { |
|
|
|
for(var i=0;i<this.listeners[type].length;i++){ |
|
|
|
for(i=0;i<this.listeners[type].length;i++){ |
|
|
|
this.listeners[type][i](data); |
|
|
|
this.listeners[type][i](data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
for(var i=0;i<this.typelessListeners.length;i++){ |
|
|
|
for(i=0;i<this.typelessListeners.length;i++){ |
|
|
|
this.typelessListeners[i](data); |
|
|
|
this.typelessListeners[i](data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -112,7 +122,7 @@ OC.EventSource.prototype={ |
|
|
|
this.listeners[type].push(callback); |
|
|
|
this.listeners[type].push(callback); |
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
this.source.addEventListener(type,function(e){ |
|
|
|
this.source.addEventListener(type,function(e){ |
|
|
|
if (typeof e.data != 'undefined') { |
|
|
|
if (typeof e.data !== 'undefined') { |
|
|
|
callback(JSON.parse(e.data)); |
|
|
|
callback(JSON.parse(e.data)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
callback(''); |
|
|
|
callback(''); |
|
|
@ -120,12 +130,13 @@ OC.EventSource.prototype={ |
|
|
|
},false); |
|
|
|
},false); |
|
|
|
} |
|
|
|
} |
|
|
|
}else{ |
|
|
|
}else{ |
|
|
|
typelessListeners.push(callback); |
|
|
|
this.typelessListeners.push(callback); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
close:function(){ |
|
|
|
close:function(){ |
|
|
|
if (typeof this.source !='undefined') { |
|
|
|
this.closed = true; |
|
|
|
|
|
|
|
if (typeof this.source !== 'undefined') { |
|
|
|
this.source.close(); |
|
|
|
this.source.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|