faye 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of faye might be problematic. Click here for more details.
- data/History.txt +7 -0
- data/Manifest.txt +1 -0
- data/build/faye-client-min.js +1 -1
- data/build/faye.js +115 -121
- data/examples/node/app.js +3 -3
- data/examples/node/faye-client-min.js +1 -1
- data/examples/node/faye.js +115 -121
- data/jake.yml +1 -1
- data/lib/faye-client-min.js +1 -1
- data/lib/faye.rb +12 -4
- data/lib/faye/client.rb +45 -10
- data/lib/faye/connection.rb +1 -1
- data/lib/faye/logging.rb +52 -0
- data/lib/faye/rack_adapter.rb +0 -1
- data/lib/faye/server.rb +68 -45
- data/lib/faye/transport.rb +8 -1
- data/test/scenario.js +5 -2
- data/test/scenario.rb +13 -1
- metadata +85 -35
data/History.txt
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
=== 0.3.3 / 2010-06-07
|
2
|
+
|
3
|
+
* Bring Node APIs up to date with 0.1.97
|
4
|
+
* Catch ECONNREFUSED errors in Node clients to withstand server outages
|
5
|
+
* Refactor the Server internals
|
6
|
+
|
7
|
+
|
1
8
|
=== 0.3.2 / 2010-04-04
|
2
9
|
|
3
10
|
* Fix problems with JSON serialization when Prototype, MooTools present
|
data/Manifest.txt
CHANGED
data/build/faye-client-min.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
if(!this.Faye)Faye={};Faye.extend=function(a,b,c){if(!b)return a;for(var d in b){if(!b.hasOwnProperty(d))continue;if(a.hasOwnProperty(d)&&c===false)continue;if(a[d]!==b[d])a[d]=b[d]}return a};Faye.extend(Faye,{VERSION:'0.3.2',BAYEUX_VERSION:'1.0',ID_LENGTH:128,JSONP_CALLBACK:'jsonpcallback',CONNECTION_TYPES:["long-polling","callback-polling"],ENV:this,random:function(a){a=a||this.ID_LENGTH;if(a>32){var b=Math.ceil(a/32),c='';while(b--)c+=this.random(32);return c}var d=Math.pow(2,a);return Math.floor(Math.random()*d).toString(16)},Grammar:{LOWALPHA:/^[a-z]$/,UPALPHA:/^[A-Z]$/,ALPHA:/^([a-z]|[A-Z])$/,DIGIT:/^[0-9]$/,ALPHANUM:/^(([a-z]|[A-Z])|[0-9])$/,MARK:/^(\-|\_|\!|\~|\(|\)|\$|\@)$/,STRING:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*$/,TOKEN:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+$/,INTEGER:/^([0-9])+$/,CHANNEL_SEGMENT:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+$/,CHANNEL_SEGMENTS:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$/,CHANNEL_NAME:/^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$/,WILD_CARD:/^\*{1,2}$/,CHANNEL_PATTERN:/^(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*\/\*{1,2}$/,VERSION_ELEMENT:/^(([a-z]|[A-Z])|[0-9])(((([a-z]|[A-Z])|[0-9])|\-|\_))*$/,VERSION:/^([0-9])+(\.(([a-z]|[A-Z])|[0-9])(((([a-z]|[A-Z])|[0-9])|\-|\_))*)*$/,CLIENT_ID:/^((([a-z]|[A-Z])|[0-9]))+$/,ID:/^((([a-z]|[A-Z])|[0-9]))+$/,ERROR_MESSAGE:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*$/,ERROR_ARGS:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*(,(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)*$/,ERROR_CODE:/^[0-9][0-9][0-9]$/,ERROR:/^([0-9][0-9][0-9]:(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*(,(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)*:(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*|[0-9][0-9][0-9]::(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)$/},commonElement:function(a,b){for(var c=0,d=a.length;c<d;c++){if(this.indexOf(b,a[c])!==-1)return a[c]}return null},indexOf:function(a,b){for(var c=0,d=a.length;c<d;c++){if(a[c]===b)return c}return-1},each:function(a,b,c){if(a instanceof Array){for(var d=0,f=a.length;d<f;d++){if(a[d]!==undefined)b.call(c||null,a[d],d)}}else{for(var g in a){if(a.hasOwnProperty(g))b.call(c||null,g,a[g])}}},filter:function(a,b,c){var d=[];this.each(a,function(){if(b.apply(c,arguments))d.push(arguments[0])});return d},size:function(a){var b=0;this.each(a,function(){b+=1});return b},enumEqual:function(c,d){if(d instanceof Array){if(!(c instanceof Array))return false;var f=c.length;if(f!==d.length)return false;while(f--){if(c[f]!==d[f])return false}return true}else{if(!(c instanceof Object))return false;if(this.size(d)!==this.size(c))return false;var g=true;this.each(c,function(a,b){g=g&&(d[a]===b)});return g}},toJSON:function(a){if(this.stringify)return this.stringify(a,function(key,value){return(this[key]instanceof Array)?this[key]:value});return JSON.stringify(a)},timestamp:function(){var b=new Date(),c=b.getFullYear(),d=b.getMonth()+1,f=b.getDate(),g=b.getHours(),j=b.getMinutes(),i=b.getSeconds();var h=function(a){return a<10?'0'+a:String(a)};return h(c)+'-'+h(d)+'-'+h(f)+' '+h(g)+':'+h(j)+':'+h(i)}});Faye.Class=function(a,b){if(typeof a!=='function'){b=a;a=Object}var c=function(){if(!this.initialize)return this;return this.initialize.apply(this,arguments)||this};var d=function(){};d.prototype=a.prototype;c.prototype=new d();Faye.extend(c.prototype,b);return c};Faye.Deferrable={callback:function(a,b){if(this._w==='succeeded')return a.apply(b,this._o);this._a=this._a||[];this._a.push([a,b])},setDeferredStatus:function(){var b=Array.prototype.slice.call(arguments),c=b.shift();this._w=c;this._o=b;if(c!=='succeeded')return;if(!this._a)return;Faye.each(this._a,function(a){a[0].apply(a[1],this._o)},this);this._a=[]}};Faye.Observable={on:function(a,b,c){this._2=this._2||{};var d=this._2[a]=this._2[a]||[];d.push([b,c])},stopObserving:function(a,b,c){if(!this._2||!this._2[a])return;if(!b){delete this._2[a];return}var d=this._2[a],f=d.length;while(f--){if(b&&d[f][0]!==b)continue;if(c&&d[f][1]!==c)continue;d.splice(f,1)}},fire:function(){var b=Array.prototype.slice.call(arguments),c=b.shift();if(!this._2||!this._2[c])return;Faye.each(this._2[c],function(a){a[0].apply(a[1],b.slice())})}};Faye.Logging={LOG_LEVELS:{error:3,warn:2,info:1,debug:0},logLevel:'error',log:function(a,b){if(!Faye.logger)return;var c=Faye.Logging.LOG_LEVELS;if(c[Faye.Logging.logLevel]>c[b])return;var d='['+b.toUpperCase()+'] [Faye',f=null;for(var g in Faye){if(f)continue;if(typeof Faye[g]!=='function')continue;if(this instanceof Faye[g])f=g}if(f)d+='.'+f;d+='] ';Faye.logger(Faye.timestamp()+' '+d+a)},error:function(a){this.log(a,'error')},warn:function(a){this.log(a,'warn')},info:function(a){this.log(a,'info')},debug:function(a){this.log(a,'debug')}};Faye.Timeouts={addTimeout:function(a,b,c,d){this._6=this._6||{};if(this._6.hasOwnProperty(a))return;var f=this;this._6[a]=setTimeout(function(){delete f._6[a];c.call(d)},1000*b)},removeTimeout:function(a){this._6=this._6||{};var b=this._6[a];if(!b)return;clearTimeout(b);delete this._6[a]}};Faye.Channel=Faye.Class({initialize:function(a){this.__id=this.name=a},push:function(a){this.fire('message',a)}});Faye.extend(Faye.Channel.prototype,Faye.Observable);Faye.extend(Faye.Channel,{HANDSHAKE:'/meta/handshake',CONNECT:'/meta/connect',SUBSCRIBE:'/meta/subscribe',UNSUBSCRIBE:'/meta/unsubscribe',DISCONNECT:'/meta/disconnect',META:'meta',SERVICE:'service',isValid:function(a){return Faye.Grammar.CHANNEL_NAME.test(a)||Faye.Grammar.CHANNEL_PATTERN.test(a)},parse:function(a){if(!this.isValid(a))return null;return a.split('/').slice(1)},isMeta:function(a){var b=this.parse(a);return b?(b[0]===this.META):null},isService:function(a){var b=this.parse(a);return b?(b[0]===this.SERVICE):null},isSubscribable:function(a){if(!this.isValid(a))return null;return!this.isMeta(a)&&!this.isService(a)},Tree:Faye.Class({initialize:function(a){this._3=a;this._8={}},eachChild:function(c,d){Faye.each(this._8,function(a,b){c.call(d,a,b)})},each:function(c,d,f){this.eachChild(function(a,b){a=c.concat(a);b.each(a,d,f)});if(this._3!==undefined)d.call(f,c,this._3)},getKeys:function(){return this.map(function(a,b){return'/'+a.join('/')})},map:function(c,d){var f=[];this.each([],function(a,b){f.push(c.call(d,a,b))});return f},get:function(a){var b=this.traverse(a);return b?b._3:null},set:function(a,b){var c=this.traverse(a,true);if(c)c._3=b},traverse:function(a,b){if(typeof a==='string')a=Faye.Channel.parse(a);if(a===null)return null;if(a.length===0)return this;var c=this._8[a[0]];if(!c&&!b)return null;if(!c)c=this._8[a[0]]=new Faye.Channel.Tree();return c.traverse(a.slice(1),b)},findOrCreate:function(a){var b=this.get(a);if(b)return b;b=new Faye.Channel(a);this.set(a,b);return b},glob:function(f){if(typeof f==='string')f=Faye.Channel.parse(f);if(f===null)return[];if(f.length===0)return(this._3===undefined)?[]:[this._3];var g=[];if(Faye.enumEqual(f,['*'])){Faye.each(this._8,function(a,b){if(b._3!==undefined)g.push(b._3)});return g}if(Faye.enumEqual(f,['**'])){g=this.map(function(a,b){return b});if(this._3!==undefined)g.pop();return g}Faye.each(this._8,function(b,c){if(b!==f[0]&&b!=='*')return;var d=c.glob(f.slice(1));Faye.each(d,function(a){g.push(a)})});if(this._8['**'])g.push(this._8['**']._3);return g}})});Faye.Namespace=Faye.Class({initialize:function(){this._p={}},generate:function(){var a=Faye.random();while(this._p.hasOwnProperty(a))a=Faye.random();return this._p[a]=a}});Faye.Transport=Faye.extend(Faye.Class({initialize:function(a,b){this.debug('Created new transport for '+b);this._b=a;this._4=b},send:function(f,g,j){if(!(f instanceof Array)&&!f.id)f.id=this._b._q.generate();this.debug('Client '+this._b._0+' sending message to '+this._4+': '+Faye.toJSON(f));this.request(f,function(b){this.debug('Client '+this._b._0+' received from '+this._4+': '+Faye.toJSON(b));if(!g)return;var c=[],d=true;Faye.each([].concat(b),function(a){if(a.id===f.id){if(g.call(j,a)===false)d=false}if(a.advice)this._b.handleAdvice(a.advice);if(a.data&&a.channel)c.push(a)},this);if(d)this._b.deliverMessages(c)},this)}}),{get:function(c,d){var f=c._4;if(d===undefined)d=this.supportedConnectionTypes();var g=null;Faye.each(this._i,function(a,b){if(Faye.indexOf(d,a)<0)return;if(g)return;if(b.isUsable(f))g=b});if(!g)throw'Could not find a usable connection type for '+f;return new g(c,f)},register:function(a,b){this._i[a]=b;b.prototype.connectionType=a},_i:{},supportedConnectionTypes:function(){var c=[],d;Faye.each(this._i,function(a,b){c.push(a)});return c}});Faye.extend(Faye.Transport.prototype,Faye.Logging);Faye.Client=Faye.Class({UNCONNECTED:1,CONNECTING:2,CONNECTED:3,DISCONNECTED:4,HANDSHAKE:'handshake',RETRY:'retry',NONE:'none',CONNECTION_TIMEOUT:60.0,DEFAULT_ENDPOINT:'/bayeux',MAX_DELAY:0.1,INTERVAL:1000.0,initialize:function(a,b){this.info('New client created for '+a);this._4=a||this.DEFAULT_ENDPOINT;this._x=b||{};this._r=this._x.timeout||this.CONNECTION_TIMEOUT;this._7=Faye.Transport.get(this);this._1=this.UNCONNECTED;this._q=new Faye.Namespace();this._j=[];this._c=new Faye.Channel.Tree();this._s=[];this._9={reconnect:this.RETRY,interval:this.INTERVAL};if(!Faye.Event)return;Faye.Event.on(Faye.ENV,'beforeunload',this.disconnect,this)},handshake:function(b,c){if(this._9.reconnect===this.NONE)return;if(this._1!==this.UNCONNECTED)return;this._1=this.CONNECTING;var d=this;this.info('Initiating handshake with '+this._4);this._7.send({channel:Faye.Channel.HANDSHAKE,version:Faye.BAYEUX_VERSION,supportedConnectionTypes:Faye.Transport.supportedConnectionTypes()},function(a){if(!a.successful){this.info('Handshake unsuccessful');setTimeout(function(){d.handshake(b,c)},this._9.interval);return this._1=this.UNCONNECTED}this._1=this.CONNECTED;this._0=a.clientId;this._7=Faye.Transport.get(this,a.supportedConnectionTypes);this.info('Handshake successful: '+this._0);if(b)b.call(c)},this)},connect:function(b,c){if(this._9.reconnect===this.NONE)return;if(this._1===this.DISCONNECTED)return;if(this._9.reconnect===this.HANDSHAKE||this._1===this.UNCONNECTED){this._t();return this.handshake(function(){this.connect(b,c)},this)}if(this._1===this.CONNECTING)return this.callback(b,c);if(this._1!==this.CONNECTED)return;this.info('Calling deferred actions for '+this._0);this.setDeferredStatus('succeeded');this.setDeferredStatus('deferred');if(b)b.call(c);if(this._d)return;this._d=this._q.generate();var d=this;this.info('Initiating connection for '+this._0);this._7.send({channel:Faye.Channel.CONNECT,clientId:this._0,connectionType:this._7.connectionType,id:this._d},this._k(function(a){delete this._d;this.removeTimeout('reconnect');this.info('Closed connection for '+this._0);setTimeout(function(){d.connect()},this._9.interval)}));this._t()},disconnect:function(){if(this._1!==this.CONNECTED)return;this._1=this.DISCONNECTED;this.info('Disconnecting '+this._0);this._7.send({channel:Faye.Channel.DISCONNECT,clientId:this._0});this.info('Clearing channel listeners for '+this._0);this._c=new Faye.Channel.Tree()},subscribe:function(c,d,f){this.connect(function(){c=[].concat(c);this._l(c);this.info('Client '+this._0+' attempting to subscribe to ['+c.join(', ')+']');this._7.send({channel:Faye.Channel.SUBSCRIBE,clientId:this._0,subscription:c},this._k(function(b){if(!b.successful||!d)return;this.info('Subscription acknowledged for '+this._0+' to ['+c.join(', ')+']');c=[].concat(b.subscription);Faye.each(c,function(a){this._c.set(a,[d,f])},this)}))},this)},unsubscribe:function(c,d,f){this.connect(function(){c=[].concat(c);this._l(c);this.info('Client '+this._0+' attempting to unsubscribe from ['+c.join(', ')+']');this._7.send({channel:Faye.Channel.UNSUBSCRIBE,clientId:this._0,subscription:c},this._k(function(b){if(!b.successful)return;this.info('Unsubscription acknowledged for '+this._0+' from ['+c.join(', ')+']');c=[].concat(b.subscription);Faye.each(c,function(a){this._c.set(a,null)},this)}))},this)},publish:function(a,b){this.connect(function(){this._l([a]);this.info('Client '+this._0+' queueing published message to '+a+': '+Faye.toJSON(b));this._y({channel:a,data:b,clientId:this._0});this.addTimeout('publish',this.MAX_DELAY,this._z,this)},this)},handleAdvice:function(a){Faye.extend(this._9,a);if(this._9.reconnect===this.HANDSHAKE)this._0=null},deliverMessages:function(d){Faye.each(d,function(b){this.info('Client '+this._0+' calling listeners for '+b.channel+' with '+Faye.toJSON(b.data));var c=this._c.glob(b.channel);Faye.each(c,function(a){if(!a)return;a[0].call(a[1],b.data)})},this)},_t:function(){this.addTimeout('reconnect',this._r,function(){delete this._d;delete this._0;this._1=this.UNCONNECTED;this.info('Server took >'+this._r+'s to reply to connection for '+this._0+': attempting to reconnect');this.subscribe(this._c.getKeys())},this)},_y:function(a){this._j.push(a)},_z:function(){this._7.send(this._j);this._j=[]},_l:function(b){Faye.each(b,function(a){if(!Faye.Channel.isValid(a))throw'"'+a+'" is not a valid channel name';if(!Faye.Channel.isSubscribable(a))throw'Clients may not subscribe to channel "'+a+'"';})},_k:function(b){var c=this;return function(a){if(a.clientId!==c._0)return false;b.call(c,a);return true}}});Faye.extend(Faye.Client.prototype,Faye.Deferrable);Faye.extend(Faye.Client.prototype,Faye.Timeouts);Faye.extend(Faye.Client.prototype,Faye.Logging);Faye.Event={_e:[],on:function(a,b,c,d){var f=function(){c.call(d)};if(a.addEventListener)a.addEventListener(b,f,false);else a.attachEvent('on'+b,f);this._e.push({_f:a,_m:b,_A:c,_g:d,_u:f})},detach:function(a,b,c,d){var f=this._e.length,g;while(f--){g=this._e[f];if((a&&a!==g._f)||(b&&b!==g._m)||(c&&c!==g._A)||(d&&d!==g._g))continue;if(g._f.removeEventListener)g._f.removeEventListener(g._m,g._u,false);else g._f.detachEvent('on'+g._m,g._u);this._e.splice(f,1);g=null}}};Faye.Event.on(Faye.ENV,'unload',Faye.Event.detach,Faye.Event);Faye.URI=Faye.extend(Faye.Class({queryString:function(){var c=[],d;Faye.each(this.params,function(a,b){c.push(encodeURIComponent(a)+'='+encodeURIComponent(b))});return c.join('&')},isLocal:function(){var a=Faye.URI.parse(Faye.ENV.location.href);var b=(a.hostname!==this.hostname)||(a.port!==this.port)||(a.protocol!==this.protocol);return!b},toURL:function(){return this.protocol+this.hostname+':'+this.port+this.pathname+'?'+this.queryString()}}),{parse:function(d,f){if(typeof d!=='string')return d;var g=new this();var j=function(b,c){d=d.replace(c,function(a){if(a)g[b]=a;return''})};j('protocol',/^https?\:\/+/);j('hostname',/^[^\/\:]+/);j('port',/^:[0-9]+/);Faye.extend(g,{protocol:'http://',hostname:Faye.ENV.location.hostname,port:Faye.ENV.location.port},false);if(!g.port)g.port=(g.protocol==='https://')?'443':'80';g.port=g.port.replace(/\D/g,'');var i=d.split('?'),h=i.shift(),l=i.join('?'),n=l?l.split('&'):[],o=n.length,k={};while(o--){i=n[o].split('=');k[decodeURIComponent(i[0]||'')]=decodeURIComponent(i[1]||'')}if(typeof f==='object')Faye.extend(k,f);g.pathname=h;g.params=k;return g}});Faye.XHR={request:function(a,b,c,d,f){var g=new this.Request(a,b,c,d,f);g.send();return g},getXhrObject:function(){return Faye.ENV.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest()},Request:Faye.Class({initialize:function(a,b,c,d,f){this._h=a.toUpperCase();this._4=Faye.URI.parse(b,c);this._B=(typeof c==='string')?c:null;this._s=(typeof d==='function')?{success:d}:d;this._g=f||null;this._5=null},send:function(){if(this._n)return;var a=this._4.pathname,b=this._4.queryString();if(this._h==='GET')a+='?'+b;var c=(this._h==='POST')?(this._B||b):'';this._n=true;this._5=Faye.XHR.getXhrObject();this._5.open(this._h,a,true);if(this._h==='POST')this._5.setRequestHeader('Content-Type','application/json');var d=this,f=function(){if(d._5.readyState!==4)return;if(g){clearInterval(g);g=null}Faye.Event.detach(Faye.ENV,'beforeunload',d.abort,d);d._n=false;d._C();d=null};var g=setInterval(f,10);Faye.Event.on(Faye.ENV,'beforeunload',this.abort,this);this._5.send(c)},abort:function(){this._5.abort()},_C:function(){var a=this._s;if(!a)return;return this.success()?a.success&&a.success.call(this._g,this):a.failure&&a.failure.call(this._g,this)},waiting:function(){return!!this._n},complete:function(){return this._5&&!this.waiting()},success:function(){if(!this.complete())return false;var a=this._5.status;return(a>=200&&a<300)||a===304||a===1223},failure:function(){if(!this.complete())return false;return!this.success()},text:function(){if(!this.complete())return null;return this._5.responseText},status:function(){if(!this.complete())return null;return this._5.status}})};if(!this.JSON){JSON={}}(function(){function l(a){return a<10?'0'+a:a}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(a){return this.getUTCFullYear()+'-'+l(this.getUTCMonth()+1)+'-'+l(this.getUTCDate())+'T'+l(this.getUTCHours())+':'+l(this.getUTCMinutes())+':'+l(this.getUTCSeconds())+'Z'};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var n=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,o=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,k,p,s={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},m;function r(c){o.lastIndex=0;return o.test(c)?'"'+c.replace(o,function(a){var b=s[a];return typeof b==='string'?b:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+c+'"'}function q(a,b){var c,d,f,g,j=k,i,h=b[a];if(h&&typeof h==='object'&&typeof h.toJSON==='function'){h=h.toJSON(a)}if(typeof m==='function'){h=m.call(b,a,h)}switch(typeof h){case'string':return r(h);case'number':return isFinite(h)?String(h):'null';case'boolean':case'null':return String(h);case'object':if(!h){return'null'}k+=p;i=[];if(Object.prototype.toString.apply(h)==='[object Array]'){g=h.length;for(c=0;c<g;c+=1){i[c]=q(c,h)||'null'}f=i.length===0?'[]':k?'[\n'+k+i.join(',\n'+k)+'\n'+j+']':'['+i.join(',')+']';k=j;return f}if(m&&typeof m==='object'){g=m.length;for(c=0;c<g;c+=1){d=m[c];if(typeof d==='string'){f=q(d,h);if(f){i.push(r(d)+(k?': ':':')+f)}}}}else{for(d in h){if(Object.hasOwnProperty.call(h,d)){f=q(d,h);if(f){i.push(r(d)+(k?': ':':')+f)}}}}f=i.length===0?'{}':k?'{\n'+k+i.join(',\n'+k)+'\n'+j+'}':'{'+i.join(',')+'}';k=j;return f}}Faye.stringify=function(a,b,c){var d;k='';p='';if(typeof c==='number'){for(d=0;d<c;d+=1){p+=' '}}else if(typeof c==='string'){p=c}m=b;if(b&&typeof b!=='function'&&(typeof b!=='object'||typeof b.length!=='number')){throw new Error('JSON.stringify');}return q('',{'':a})};if(typeof JSON.stringify!=='function'){JSON.stringify=Faye.stringify}if(typeof JSON.parse!=='function'){JSON.parse=function(g,j){var i;function h(a,b){var c,d,f=a[b];if(f&&typeof f==='object'){for(c in f){if(Object.hasOwnProperty.call(f,c)){d=h(f,c);if(d!==undefined){f[c]=d}else{delete f[c]}}}}return j.call(a,b,f)}n.lastIndex=0;if(n.test(g)){g=g.replace(n,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(g.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){i=eval('('+g+')');return typeof j==='function'?h({'':i},''):i}throw new SyntaxError('JSON.parse');}}}());Faye.XHRTransport=Faye.Class(Faye.Transport,{request:function(b,c,d){Faye.XHR.request('post',this._4,Faye.toJSON(b),function(a){if(c)c.call(d,JSON.parse(a.text()))})}});Faye.XHRTransport.isUsable=function(a){return Faye.URI.parse(a).isLocal()};Faye.Transport.register('long-polling',Faye.XHRTransport);Faye.JSONPTransport=Faye.extend(Faye.Class(Faye.Transport,{request:function(b,c,d){var f={message:Faye.toJSON(b)},g=document.getElementsByTagName('head')[0],j=document.createElement('script'),i=Faye.JSONPTransport.getCallbackName(),h=Faye.URI.parse(this._4,f);Faye.ENV[i]=function(a){Faye.ENV[i]=undefined;try{delete Faye.ENV[i]}catch(e){}g.removeChild(j);if(c)c.call(d,a)};h.params.jsonp=i;j.type='text/javascript';j.src=h.toURL();g.appendChild(j)}}),{_v:0,getCallbackName:function(){this._v+=1;return'__jsonp'+this._v+'__'}});Faye.JSONPTransport.isUsable=function(a){return true};Faye.Transport.register('callback-polling',Faye.JSONPTransport);
|
1
|
+
if(!this.Faye)Faye={};Faye.extend=function(a,b,c){if(!b)return a;for(var d in b){if(!b.hasOwnProperty(d))continue;if(a.hasOwnProperty(d)&&c===false)continue;if(a[d]!==b[d])a[d]=b[d]}return a};Faye.extend(Faye,{VERSION:'0.3.3',BAYEUX_VERSION:'1.0',ID_LENGTH:128,JSONP_CALLBACK:'jsonpcallback',CONNECTION_TYPES:["long-polling","callback-polling"],ENV:this,random:function(a){a=a||this.ID_LENGTH;if(a>32){var b=Math.ceil(a/32),c='';while(b--)c+=this.random(32);return c}var d=Math.pow(2,a);return Math.floor(Math.random()*d).toString(16)},Grammar:{LOWALPHA:/^[a-z]$/,UPALPHA:/^[A-Z]$/,ALPHA:/^([a-z]|[A-Z])$/,DIGIT:/^[0-9]$/,ALPHANUM:/^(([a-z]|[A-Z])|[0-9])$/,MARK:/^(\-|\_|\!|\~|\(|\)|\$|\@)$/,STRING:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*$/,TOKEN:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+$/,INTEGER:/^([0-9])+$/,CHANNEL_SEGMENT:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+$/,CHANNEL_SEGMENTS:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$/,CHANNEL_NAME:/^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$/,WILD_CARD:/^\*{1,2}$/,CHANNEL_PATTERN:/^(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*\/\*{1,2}$/,VERSION_ELEMENT:/^(([a-z]|[A-Z])|[0-9])(((([a-z]|[A-Z])|[0-9])|\-|\_))*$/,VERSION:/^([0-9])+(\.(([a-z]|[A-Z])|[0-9])(((([a-z]|[A-Z])|[0-9])|\-|\_))*)*$/,CLIENT_ID:/^((([a-z]|[A-Z])|[0-9]))+$/,ID:/^((([a-z]|[A-Z])|[0-9]))+$/,ERROR_MESSAGE:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*$/,ERROR_ARGS:/^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*(,(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)*$/,ERROR_CODE:/^[0-9][0-9][0-9]$/,ERROR:/^([0-9][0-9][0-9]:(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*(,(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)*:(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*|[0-9][0-9][0-9]::(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)$/},commonElement:function(a,b){for(var c=0,d=a.length;c<d;c++){if(this.indexOf(b,a[c])!==-1)return a[c]}return null},indexOf:function(a,b){for(var c=0,d=a.length;c<d;c++){if(a[c]===b)return c}return-1},each:function(a,b,c){if(a instanceof Array){for(var d=0,f=a.length;d<f;d++){if(a[d]!==undefined)b.call(c||null,a[d],d)}}else{for(var g in a){if(a.hasOwnProperty(g))b.call(c||null,g,a[g])}}},filter:function(a,b,c){var d=[];this.each(a,function(){if(b.apply(c,arguments))d.push(arguments[0])});return d},size:function(a){var b=0;this.each(a,function(){b+=1});return b},enumEqual:function(c,d){if(d instanceof Array){if(!(c instanceof Array))return false;var f=c.length;if(f!==d.length)return false;while(f--){if(c[f]!==d[f])return false}return true}else{if(!(c instanceof Object))return false;if(this.size(d)!==this.size(c))return false;var g=true;this.each(c,function(a,b){g=g&&(d[a]===b)});return g}},toJSON:function(a){if(this.stringify)return this.stringify(a,function(key,value){return(this[key]instanceof Array)?this[key]:value});return JSON.stringify(a)},timestamp:function(){var b=new Date(),c=b.getFullYear(),d=b.getMonth()+1,f=b.getDate(),g=b.getHours(),j=b.getMinutes(),i=b.getSeconds();var h=function(a){return a<10?'0'+a:String(a)};return h(c)+'-'+h(d)+'-'+h(f)+' '+h(g)+':'+h(j)+':'+h(i)}});Faye.Class=function(a,b){if(typeof a!=='function'){b=a;a=Object}var c=function(){if(!this.initialize)return this;return this.initialize.apply(this,arguments)||this};var d=function(){};d.prototype=a.prototype;c.prototype=new d();Faye.extend(c.prototype,b);return c};Faye.Deferrable={callback:function(a,b){if(this._v==='succeeded')return a.apply(b,this._o);this._a=this._a||[];this._a.push([a,b])},setDeferredStatus:function(){var b=Array.prototype.slice.call(arguments),c=b.shift();this._v=c;this._o=b;if(c!=='succeeded')return;if(!this._a)return;Faye.each(this._a,function(a){a[0].apply(a[1],this._o)},this);this._a=[]}};Faye.Observable={on:function(a,b,c){this._2=this._2||{};var d=this._2[a]=this._2[a]||[];d.push([b,c])},stopObserving:function(a,b,c){if(!this._2||!this._2[a])return;if(!b){delete this._2[a];return}var d=this._2[a],f=d.length;while(f--){if(b&&d[f][0]!==b)continue;if(c&&d[f][1]!==c)continue;d.splice(f,1)}},trigger:function(){var b=Array.prototype.slice.call(arguments),c=b.shift();if(!this._2||!this._2[c])return;Faye.each(this._2[c],function(a){a[0].apply(a[1],b.slice())})}};Faye.Logging={LOG_LEVELS:{error:3,warn:2,info:1,debug:0},logLevel:'error',log:function(a,b){if(!Faye.logger)return;var c=Faye.Logging.LOG_LEVELS;if(c[Faye.Logging.logLevel]>c[b])return;var a=Array.prototype.slice.apply(a),d=' ['+b.toUpperCase()+'] [Faye',f=null,g=a.shift().replace(/\?/g,function(){return Faye.toJSON(a.shift())});for(var j in Faye){if(f)continue;if(typeof Faye[j]!=='function')continue;if(this instanceof Faye[j])f=j}if(f)d+='.'+f;d+='] ';Faye.logger(Faye.timestamp()+d+g)}};Faye.each(Faye.Logging.LOG_LEVELS,function(a,b){Faye.Logging[a]=function(){this.log(arguments,a)}});Faye.Timeouts={addTimeout:function(a,b,c,d){this._6=this._6||{};if(this._6.hasOwnProperty(a))return;var f=this;this._6[a]=setTimeout(function(){delete f._6[a];c.call(d)},1000*b)},removeTimeout:function(a){this._6=this._6||{};var b=this._6[a];if(!b)return;clearTimeout(b);delete this._6[a]}};Faye.Channel=Faye.Class({initialize:function(a){this.__id=this.name=a},push:function(a){this.trigger('message',a)}});Faye.extend(Faye.Channel.prototype,Faye.Observable);Faye.extend(Faye.Channel,{HANDSHAKE:'/meta/handshake',CONNECT:'/meta/connect',SUBSCRIBE:'/meta/subscribe',UNSUBSCRIBE:'/meta/unsubscribe',DISCONNECT:'/meta/disconnect',META:'meta',SERVICE:'service',isValid:function(a){return Faye.Grammar.CHANNEL_NAME.test(a)||Faye.Grammar.CHANNEL_PATTERN.test(a)},parse:function(a){if(!this.isValid(a))return null;return a.split('/').slice(1)},isMeta:function(a){var b=this.parse(a);return b?(b[0]===this.META):null},isService:function(a){var b=this.parse(a);return b?(b[0]===this.SERVICE):null},isSubscribable:function(a){if(!this.isValid(a))return null;return!this.isMeta(a)&&!this.isService(a)},Tree:Faye.Class({initialize:function(a){this._3=a;this._8={}},eachChild:function(c,d){Faye.each(this._8,function(a,b){c.call(d,a,b)})},each:function(c,d,f){this.eachChild(function(a,b){a=c.concat(a);b.each(a,d,f)});if(this._3!==undefined)d.call(f,c,this._3)},getKeys:function(){return this.map(function(a,b){return'/'+a.join('/')})},map:function(c,d){var f=[];this.each([],function(a,b){f.push(c.call(d,a,b))});return f},get:function(a){var b=this.traverse(a);return b?b._3:null},set:function(a,b){var c=this.traverse(a,true);if(c)c._3=b},traverse:function(a,b){if(typeof a==='string')a=Faye.Channel.parse(a);if(a===null)return null;if(a.length===0)return this;var c=this._8[a[0]];if(!c&&!b)return null;if(!c)c=this._8[a[0]]=new Faye.Channel.Tree();return c.traverse(a.slice(1),b)},findOrCreate:function(a){var b=this.get(a);if(b)return b;b=new Faye.Channel(a);this.set(a,b);return b},glob:function(f){if(typeof f==='string')f=Faye.Channel.parse(f);if(f===null)return[];if(f.length===0)return(this._3===undefined)?[]:[this._3];var g=[];if(Faye.enumEqual(f,['*'])){Faye.each(this._8,function(a,b){if(b._3!==undefined)g.push(b._3)});return g}if(Faye.enumEqual(f,['**'])){g=this.map(function(a,b){return b});if(this._3!==undefined)g.pop();return g}Faye.each(this._8,function(b,c){if(b!==f[0]&&b!=='*')return;var d=c.glob(f.slice(1));Faye.each(d,function(a){g.push(a)})});if(this._8['**'])g.push(this._8['**']._3);return g}})});Faye.Namespace=Faye.Class({initialize:function(){this._p={}},generate:function(){var a=Faye.random();while(this._p.hasOwnProperty(a))a=Faye.random();return this._p[a]=a}});Faye.Transport=Faye.extend(Faye.Class({initialize:function(a,b){this.debug('Created new transport for ?',b);this._b=a;this._4=b},send:function(f,g,j){if(!(f instanceof Array)&&!f.id)f.id=this._b._q.generate();this.debug('Client ? sending message to ?: ?',this._b._0,this._4,f);this.request(f,function(b){this.debug('Client ? received from ?: ?',this._b._0,this._4,b);if(!g)return;var c=[],d=true;Faye.each([].concat(b),function(a){if(a.id===f.id){if(g.call(j,a)===false)d=false}if(a.advice)this._b.handleAdvice(a.advice);if(a.data&&a.channel)c.push(a)},this);if(d)this._b.deliverMessages(c)},this)}}),{get:function(c,d){var f=c._4;if(d===undefined)d=this.supportedConnectionTypes();var g=null;Faye.each(this._i,function(a,b){if(Faye.indexOf(d,a)<0)return;if(g)return;if(b.isUsable(f))g=b});if(!g)throw'Could not find a usable connection type for '+f;return new g(c,f)},register:function(a,b){this._i[a]=b;b.prototype.connectionType=a},_i:{},supportedConnectionTypes:function(){var c=[],d;Faye.each(this._i,function(a,b){c.push(a)});return c}});Faye.extend(Faye.Transport.prototype,Faye.Logging);Faye.Client=Faye.Class({UNCONNECTED:1,CONNECTING:2,CONNECTED:3,DISCONNECTED:4,HANDSHAKE:'handshake',RETRY:'retry',NONE:'none',CONNECTION_TIMEOUT:60.0,DEFAULT_ENDPOINT:'/bayeux',MAX_DELAY:0.1,INTERVAL:1000.0,initialize:function(a,b){this.info('New client created for ?',a);this._4=a||this.DEFAULT_ENDPOINT;this._w=b||{};this._r=this._w.timeout||this.CONNECTION_TIMEOUT;this._7=Faye.Transport.get(this);this._1=this.UNCONNECTED;this._q=new Faye.Namespace();this._j=[];this._c=new Faye.Channel.Tree();this._9={reconnect:this.RETRY,interval:this.INTERVAL};if(Faye.Event)Faye.Event.on(Faye.ENV,'beforeunload',this.disconnect,this)},handshake:function(b,c){if(this._9.reconnect===this.NONE)return;if(this._1!==this.UNCONNECTED)return;this._1=this.CONNECTING;var d=this;this.info('Initiating handshake with ?',this._4);this._7.send({channel:Faye.Channel.HANDSHAKE,version:Faye.BAYEUX_VERSION,supportedConnectionTypes:Faye.Transport.supportedConnectionTypes()},function(a){if(a.successful){this._1=this.CONNECTED;this._0=a.clientId;this._7=Faye.Transport.get(this,a.supportedConnectionTypes);this.info('Handshake successful: ?',this._0);if(b)b.call(c)}else{this.info('Handshake unsuccessful');setTimeout(function(){d.handshake(b,c)},this._9.interval);this._1=this.UNCONNECTED}},this)},connect:function(b,c){if(this._9.reconnect===this.NONE)return;if(this._1===this.DISCONNECTED)return;if(this._9.reconnect===this.HANDSHAKE||this._1===this.UNCONNECTED){this._s();return this.handshake(function(){this.connect(b,c)},this)}if(this._1===this.CONNECTING)return this.callback(b,c);if(this._1!==this.CONNECTED)return;this.info('Calling deferred actions for ?',this._0);this.setDeferredStatus('succeeded');this.setDeferredStatus('deferred');if(b)b.call(c);if(this._d)return;this._d=this._q.generate();var d=this;this.info('Initiating connection for ?',this._0);this._7.send({channel:Faye.Channel.CONNECT,clientId:this._0,connectionType:this._7.connectionType,id:this._d},this._k(function(a){this._d=null;this.removeTimeout('reconnect');this.info('Closed connection for ?',this._0);setTimeout(function(){d.connect()},this._9.interval)}));this._s()},disconnect:function(){if(this._1!==this.CONNECTED)return;this._1=this.DISCONNECTED;this.info('Disconnecting ?',this._0);this._7.send({channel:Faye.Channel.DISCONNECT,clientId:this._0});this.info('Clearing channel listeners for ?',this._0);this._c=new Faye.Channel.Tree();this.removeTimeout('reconnect')},subscribe:function(c,d,f){this.connect(function(){c=[].concat(c);this._l(c);this.info('Client ? attempting to subscribe to ?',this._0,c);this._7.send({channel:Faye.Channel.SUBSCRIBE,clientId:this._0,subscription:c},this._k(function(b){if(!b.successful||!d)return;this.info('Subscription acknowledged for ? to ?',this._0,c);c=[].concat(b.subscription);Faye.each(c,function(a){this._c.set(a,[d,f])},this)}))},this)},unsubscribe:function(c,d,f){this.connect(function(){c=[].concat(c);this._l(c);this.info('Client ? attempting to unsubscribe from ?',this._0,c);this._7.send({channel:Faye.Channel.UNSUBSCRIBE,clientId:this._0,subscription:c},this._k(function(b){if(!b.successful)return;this.info('Unsubscription acknowledged for ? from ?',this._0,c);c=[].concat(b.subscription);Faye.each(c,function(a){this._c.set(a,undefined)},this)}))},this)},publish:function(a,b){this.connect(function(){this._l([a]);this.info('Client ? queueing published message to ?: ?',this._0,a,b);this._x({channel:a,data:b,clientId:this._0});this.addTimeout('publish',this.MAX_DELAY,this._y,this)},this)},handleAdvice:function(a){Faye.extend(this._9,a);if(this._9.reconnect===this.HANDSHAKE)this._0=null},deliverMessages:function(d){Faye.each(d,function(b){this.info('Client ? calling listeners for ? with ?',this._0,b.channel,b.data);var c=this._c.glob(b.channel);Faye.each(c,function(a){a[0].call(a[1],b.data)})},this)},_s:function(){this.addTimeout('reconnect',this._r,function(){this._d=null;this._0=null;this._1=this.UNCONNECTED;this.info('Server took >?s to reply to connection for ?: attempting to reconnect',this._r,this._0);this.subscribe(this._c.getKeys())},this)},_x:function(a){this._j.push(a)},_y:function(){this._7.send(this._j);this._j=[]},_l:function(b){Faye.each(b,function(a){if(!Faye.Channel.isValid(a))throw'"'+a+'" is not a valid channel name';if(!Faye.Channel.isSubscribable(a))throw'Clients may not subscribe to channel "'+a+'"';})},_k:function(b){var c=this;return function(a){if(a.clientId!==c._0)return false;b.call(c,a);return true}}});Faye.extend(Faye.Client.prototype,Faye.Deferrable);Faye.extend(Faye.Client.prototype,Faye.Timeouts);Faye.extend(Faye.Client.prototype,Faye.Logging);Faye.Event={_e:[],on:function(a,b,c,d){var f=function(){c.call(d)};if(a.addEventListener)a.addEventListener(b,f,false);else a.attachEvent('on'+b,f);this._e.push({_f:a,_m:b,_z:c,_g:d,_t:f})},detach:function(a,b,c,d){var f=this._e.length,g;while(f--){g=this._e[f];if((a&&a!==g._f)||(b&&b!==g._m)||(c&&c!==g._z)||(d&&d!==g._g))continue;if(g._f.removeEventListener)g._f.removeEventListener(g._m,g._t,false);else g._f.detachEvent('on'+g._m,g._t);this._e.splice(f,1);g=null}}};Faye.Event.on(Faye.ENV,'unload',Faye.Event.detach,Faye.Event);Faye.URI=Faye.extend(Faye.Class({queryString:function(){var c=[],d;Faye.each(this.params,function(a,b){c.push(encodeURIComponent(a)+'='+encodeURIComponent(b))});return c.join('&')},isLocal:function(){var a=Faye.URI.parse(Faye.ENV.location.href);var b=(a.hostname!==this.hostname)||(a.port!==this.port)||(a.protocol!==this.protocol);return!b},toURL:function(){return this.protocol+this.hostname+':'+this.port+this.pathname+'?'+this.queryString()}}),{parse:function(d,f){if(typeof d!=='string')return d;var g=new this();var j=function(b,c){d=d.replace(c,function(a){if(a)g[b]=a;return''})};j('protocol',/^https?\:\/+/);j('hostname',/^[^\/\:]+/);j('port',/^:[0-9]+/);Faye.extend(g,{protocol:'http://',hostname:Faye.ENV.location.hostname,port:Faye.ENV.location.port},false);if(!g.port)g.port=(g.protocol==='https://')?'443':'80';g.port=g.port.replace(/\D/g,'');var i=d.split('?'),h=i.shift(),l=i.join('?'),n=l?l.split('&'):[],o=n.length,k={};while(o--){i=n[o].split('=');k[decodeURIComponent(i[0]||'')]=decodeURIComponent(i[1]||'')}if(typeof f==='object')Faye.extend(k,f);g.pathname=h;g.params=k;return g}});Faye.XHR={request:function(a,b,c,d,f){var g=new this.Request(a,b,c,d,f);g.send();return g},getXhrObject:function(){return Faye.ENV.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest()},Request:Faye.Class({initialize:function(a,b,c,d,f){this._h=a.toUpperCase();this._4=Faye.URI.parse(b,c);this._A=(typeof c==='string')?c:null;this._B=(typeof d==='function')?{success:d}:d;this._g=f||null;this._5=null},send:function(){if(this._n)return;var a=this._4.pathname,b=this._4.queryString();if(this._h==='GET')a+='?'+b;var c=(this._h==='POST')?(this._A||b):'';this._n=true;this._5=Faye.XHR.getXhrObject();this._5.open(this._h,a,true);if(this._h==='POST')this._5.setRequestHeader('Content-Type','application/json');var d=this,f=function(){if(d._5.readyState!==4)return;if(g){clearInterval(g);g=null}Faye.Event.detach(Faye.ENV,'beforeunload',d.abort,d);d._n=false;d._C();d=null};var g=setInterval(f,10);Faye.Event.on(Faye.ENV,'beforeunload',this.abort,this);this._5.send(c)},abort:function(){this._5.abort()},_C:function(){var a=this._B;if(!a)return;return this.success()?a.success&&a.success.call(this._g,this):a.failure&&a.failure.call(this._g,this)},waiting:function(){return!!this._n},complete:function(){return this._5&&!this.waiting()},success:function(){if(!this.complete())return false;var a=this._5.status;return(a>=200&&a<300)||a===304||a===1223},failure:function(){if(!this.complete())return false;return!this.success()},text:function(){if(!this.complete())return null;return this._5.responseText},status:function(){if(!this.complete())return null;return this._5.status}})};if(!this.JSON){JSON={}}(function(){function l(a){return a<10?'0'+a:a}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(a){return this.getUTCFullYear()+'-'+l(this.getUTCMonth()+1)+'-'+l(this.getUTCDate())+'T'+l(this.getUTCHours())+':'+l(this.getUTCMinutes())+':'+l(this.getUTCSeconds())+'Z'};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var n=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,o=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,k,p,s={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},m;function r(c){o.lastIndex=0;return o.test(c)?'"'+c.replace(o,function(a){var b=s[a];return typeof b==='string'?b:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+c+'"'}function q(a,b){var c,d,f,g,j=k,i,h=b[a];if(h&&typeof h==='object'&&typeof h.toJSON==='function'){h=h.toJSON(a)}if(typeof m==='function'){h=m.call(b,a,h)}switch(typeof h){case'string':return r(h);case'number':return isFinite(h)?String(h):'null';case'boolean':case'null':return String(h);case'object':if(!h){return'null'}k+=p;i=[];if(Object.prototype.toString.apply(h)==='[object Array]'){g=h.length;for(c=0;c<g;c+=1){i[c]=q(c,h)||'null'}f=i.length===0?'[]':k?'[\n'+k+i.join(',\n'+k)+'\n'+j+']':'['+i.join(',')+']';k=j;return f}if(m&&typeof m==='object'){g=m.length;for(c=0;c<g;c+=1){d=m[c];if(typeof d==='string'){f=q(d,h);if(f){i.push(r(d)+(k?': ':':')+f)}}}}else{for(d in h){if(Object.hasOwnProperty.call(h,d)){f=q(d,h);if(f){i.push(r(d)+(k?': ':':')+f)}}}}f=i.length===0?'{}':k?'{\n'+k+i.join(',\n'+k)+'\n'+j+'}':'{'+i.join(',')+'}';k=j;return f}}Faye.stringify=function(a,b,c){var d;k='';p='';if(typeof c==='number'){for(d=0;d<c;d+=1){p+=' '}}else if(typeof c==='string'){p=c}m=b;if(b&&typeof b!=='function'&&(typeof b!=='object'||typeof b.length!=='number')){throw new Error('JSON.stringify');}return q('',{'':a})};if(typeof JSON.stringify!=='function'){JSON.stringify=Faye.stringify}if(typeof JSON.parse!=='function'){JSON.parse=function(g,j){var i;function h(a,b){var c,d,f=a[b];if(f&&typeof f==='object'){for(c in f){if(Object.hasOwnProperty.call(f,c)){d=h(f,c);if(d!==undefined){f[c]=d}else{delete f[c]}}}}return j.call(a,b,f)}n.lastIndex=0;if(n.test(g)){g=g.replace(n,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(g.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){i=eval('('+g+')');return typeof j==='function'?h({'':i},''):i}throw new SyntaxError('JSON.parse');}}}());Faye.XHRTransport=Faye.Class(Faye.Transport,{request:function(b,c,d){Faye.XHR.request('post',this._4,Faye.toJSON(b),function(a){if(c)c.call(d,JSON.parse(a.text()))})}});Faye.XHRTransport.isUsable=function(a){return Faye.URI.parse(a).isLocal()};Faye.Transport.register('long-polling',Faye.XHRTransport);Faye.JSONPTransport=Faye.extend(Faye.Class(Faye.Transport,{request:function(b,c,d){var f={message:Faye.toJSON(b)},g=document.getElementsByTagName('head')[0],j=document.createElement('script'),i=Faye.JSONPTransport.getCallbackName(),h=Faye.URI.parse(this._4,f);Faye.ENV[i]=function(a){Faye.ENV[i]=undefined;try{delete Faye.ENV[i]}catch(e){}g.removeChild(j);if(c)c.call(d,a)};h.params.jsonp=i;j.type='text/javascript';j.src=h.toURL();g.appendChild(j)}}),{_u:0,getCallbackName:function(){this._u+=1;return'__jsonp'+this._u+'__'}});Faye.JSONPTransport.isUsable=function(a){return true};Faye.Transport.register('callback-polling',Faye.JSONPTransport);
|
data/build/faye.js
CHANGED
@@ -12,7 +12,7 @@ Faye.extend = function(dest, source, overwrite) {
|
|
12
12
|
};
|
13
13
|
|
14
14
|
Faye.extend(Faye, {
|
15
|
-
VERSION: '0.3.
|
15
|
+
VERSION: '0.3.3',
|
16
16
|
|
17
17
|
BAYEUX_VERSION: '1.0',
|
18
18
|
ID_LENGTH: 128,
|
@@ -249,7 +249,7 @@ Faye.Observable = {
|
|
249
249
|
}
|
250
250
|
},
|
251
251
|
|
252
|
-
|
252
|
+
trigger: function() {
|
253
253
|
var args = Array.prototype.slice.call(arguments),
|
254
254
|
eventType = args.shift();
|
255
255
|
|
@@ -272,14 +272,19 @@ Faye.Logging = {
|
|
272
272
|
|
273
273
|
logLevel: 'error',
|
274
274
|
|
275
|
-
log: function(
|
275
|
+
log: function(messageArgs, level) {
|
276
276
|
if (!Faye.logger) return;
|
277
277
|
|
278
278
|
var levels = Faye.Logging.LOG_LEVELS;
|
279
279
|
if (levels[Faye.Logging.logLevel] > levels[level]) return;
|
280
280
|
|
281
|
-
var
|
282
|
-
|
281
|
+
var messageArgs = Array.prototype.slice.apply(messageArgs),
|
282
|
+
banner = ' [' + level.toUpperCase() + '] [Faye',
|
283
|
+
klass = null,
|
284
|
+
|
285
|
+
message = messageArgs.shift().replace(/\?/g, function() {
|
286
|
+
return Faye.toJSON(messageArgs.shift());
|
287
|
+
});
|
283
288
|
|
284
289
|
for (var key in Faye) {
|
285
290
|
if (klass) continue;
|
@@ -289,23 +294,16 @@ Faye.Logging = {
|
|
289
294
|
if (klass) banner += '.' + klass;
|
290
295
|
banner += '] ';
|
291
296
|
|
292
|
-
Faye.logger(Faye.timestamp() +
|
293
|
-
},
|
294
|
-
|
295
|
-
error: function(message) {
|
296
|
-
this.log(message, 'error');
|
297
|
-
},
|
298
|
-
warn: function(message) {
|
299
|
-
this.log(message, 'warn');
|
300
|
-
},
|
301
|
-
info: function(message) {
|
302
|
-
this.log(message, 'info');
|
303
|
-
},
|
304
|
-
debug: function(message) {
|
305
|
-
this.log(message, 'debug');
|
297
|
+
Faye.logger(Faye.timestamp() + banner + message);
|
306
298
|
}
|
307
299
|
};
|
308
300
|
|
301
|
+
Faye.each(Faye.Logging.LOG_LEVELS, function(level, value) {
|
302
|
+
Faye.Logging[level] = function() {
|
303
|
+
this.log(arguments, level);
|
304
|
+
};
|
305
|
+
});
|
306
|
+
|
309
307
|
|
310
308
|
Faye.Timeouts = {
|
311
309
|
addTimeout: function(name, delay, callback, scope) {
|
@@ -334,7 +332,7 @@ Faye.Channel = Faye.Class({
|
|
334
332
|
},
|
335
333
|
|
336
334
|
push: function(message) {
|
337
|
-
this.
|
335
|
+
this.trigger('message', message);
|
338
336
|
}
|
339
337
|
});
|
340
338
|
|
@@ -488,7 +486,7 @@ Faye.Namespace = Faye.Class({
|
|
488
486
|
|
489
487
|
Faye.Transport = Faye.extend(Faye.Class({
|
490
488
|
initialize: function(client, endpoint) {
|
491
|
-
this.debug('Created new transport for '
|
489
|
+
this.debug('Created new transport for ?', endpoint);
|
492
490
|
this._client = client;
|
493
491
|
this._endpoint = endpoint;
|
494
492
|
},
|
@@ -497,14 +495,12 @@ Faye.Transport = Faye.extend(Faye.Class({
|
|
497
495
|
if (!(message instanceof Array) && !message.id)
|
498
496
|
message.id = this._client._namespace.generate();
|
499
497
|
|
500
|
-
this.debug('Client
|
501
|
-
|
502
|
-
Faye.toJSON(message));
|
498
|
+
this.debug('Client ? sending message to ?: ?',
|
499
|
+
this._client._clientId, this._endpoint, message);
|
503
500
|
|
504
501
|
this.request(message, function(responses) {
|
505
|
-
this.debug('Client
|
506
|
-
|
507
|
-
Faye.toJSON(responses));
|
502
|
+
this.debug('Client ? received from ?: ?',
|
503
|
+
this._client._clientId, this._endpoint, responses);
|
508
504
|
|
509
505
|
if (!callback) return;
|
510
506
|
|
@@ -578,7 +574,7 @@ Faye.Client = Faye.Class({
|
|
578
574
|
INTERVAL: 1000.0,
|
579
575
|
|
580
576
|
initialize: function(endpoint, options) {
|
581
|
-
this.info('New client created for '
|
577
|
+
this.info('New client created for ?', endpoint);
|
582
578
|
|
583
579
|
this._endpoint = endpoint || this.DEFAULT_ENDPOINT;
|
584
580
|
this._options = options || {};
|
@@ -589,12 +585,11 @@ Faye.Client = Faye.Class({
|
|
589
585
|
this._namespace = new Faye.Namespace();
|
590
586
|
this._outbox = [];
|
591
587
|
this._channels = new Faye.Channel.Tree();
|
592
|
-
this._callbacks = [];
|
593
588
|
|
594
589
|
this._advice = {reconnect: this.RETRY, interval: this.INTERVAL};
|
595
590
|
|
596
|
-
if (
|
597
|
-
|
591
|
+
if (Faye.Event) Faye.Event.on(Faye.ENV, 'beforeunload',
|
592
|
+
this.disconnect, this);
|
598
593
|
},
|
599
594
|
|
600
595
|
// Request
|
@@ -623,7 +618,7 @@ Faye.Client = Faye.Class({
|
|
623
618
|
this._state = this.CONNECTING;
|
624
619
|
var self = this;
|
625
620
|
|
626
|
-
this.info('Initiating handshake with '
|
621
|
+
this.info('Initiating handshake with ?', this._endpoint);
|
627
622
|
|
628
623
|
this._transport.send({
|
629
624
|
channel: Faye.Channel.HANDSHAKE,
|
@@ -632,18 +627,19 @@ Faye.Client = Faye.Class({
|
|
632
627
|
|
633
628
|
}, function(response) {
|
634
629
|
|
635
|
-
if (
|
630
|
+
if (response.successful) {
|
631
|
+
this._state = this.CONNECTED;
|
632
|
+
this._clientId = response.clientId;
|
633
|
+
this._transport = Faye.Transport.get(this, response.supportedConnectionTypes);
|
634
|
+
|
635
|
+
this.info('Handshake successful: ?', this._clientId);
|
636
|
+
if (callback) callback.call(scope);
|
637
|
+
|
638
|
+
} else {
|
636
639
|
this.info('Handshake unsuccessful');
|
637
640
|
setTimeout(function() { self.handshake(callback, scope) }, this._advice.interval);
|
638
|
-
|
641
|
+
this._state = this.UNCONNECTED;
|
639
642
|
}
|
640
|
-
|
641
|
-
this._state = this.CONNECTED;
|
642
|
-
this._clientId = response.clientId;
|
643
|
-
this._transport = Faye.Transport.get(this, response.supportedConnectionTypes);
|
644
|
-
|
645
|
-
this.info('Handshake successful: ' + this._clientId);
|
646
|
-
if (callback) callback.call(scope);
|
647
643
|
}, this);
|
648
644
|
},
|
649
645
|
|
@@ -670,7 +666,7 @@ Faye.Client = Faye.Class({
|
|
670
666
|
|
671
667
|
if (this._state !== this.CONNECTED) return;
|
672
668
|
|
673
|
-
this.info('Calling deferred actions for '
|
669
|
+
this.info('Calling deferred actions for ?', this._clientId);
|
674
670
|
this.setDeferredStatus('succeeded');
|
675
671
|
this.setDeferredStatus('deferred');
|
676
672
|
if (callback) callback.call(scope);
|
@@ -678,7 +674,7 @@ Faye.Client = Faye.Class({
|
|
678
674
|
if (this._connectionId) return;
|
679
675
|
this._connectionId = this._namespace.generate();
|
680
676
|
var self = this;
|
681
|
-
this.info('Initiating connection for '
|
677
|
+
this.info('Initiating connection for ?', this._clientId);
|
682
678
|
|
683
679
|
this._transport.send({
|
684
680
|
channel: Faye.Channel.CONNECT,
|
@@ -687,10 +683,10 @@ Faye.Client = Faye.Class({
|
|
687
683
|
id: this._connectionId
|
688
684
|
|
689
685
|
}, this._verifyClientId(function(response) {
|
690
|
-
|
686
|
+
this._connectionId = null;
|
691
687
|
this.removeTimeout('reconnect');
|
692
688
|
|
693
|
-
this.info('Closed connection for '
|
689
|
+
this.info('Closed connection for ?', this._clientId);
|
694
690
|
setTimeout(function() { self.connect() }, this._advice.interval);
|
695
691
|
}));
|
696
692
|
|
@@ -708,15 +704,16 @@ Faye.Client = Faye.Class({
|
|
708
704
|
if (this._state !== this.CONNECTED) return;
|
709
705
|
this._state = this.DISCONNECTED;
|
710
706
|
|
711
|
-
this.info('Disconnecting '
|
707
|
+
this.info('Disconnecting ?', this._clientId);
|
712
708
|
|
713
709
|
this._transport.send({
|
714
710
|
channel: Faye.Channel.DISCONNECT,
|
715
711
|
clientId: this._clientId
|
716
712
|
});
|
717
713
|
|
718
|
-
this.info('Clearing channel listeners for '
|
714
|
+
this.info('Clearing channel listeners for ?', this._clientId);
|
719
715
|
this._channels = new Faye.Channel.Tree();
|
716
|
+
this.removeTimeout('reconnect');
|
720
717
|
},
|
721
718
|
|
722
719
|
// Request Response
|
@@ -735,8 +732,7 @@ Faye.Client = Faye.Class({
|
|
735
732
|
channels = [].concat(channels);
|
736
733
|
this._validateChannels(channels);
|
737
734
|
|
738
|
-
this.info('Client
|
739
|
-
channels.join(', ') + ']');
|
735
|
+
this.info('Client ? attempting to subscribe to ?', this._clientId, channels);
|
740
736
|
|
741
737
|
this._transport.send({
|
742
738
|
channel: Faye.Channel.SUBSCRIBE,
|
@@ -746,8 +742,7 @@ Faye.Client = Faye.Class({
|
|
746
742
|
}, this._verifyClientId(function(response) {
|
747
743
|
if (!response.successful || !callback) return;
|
748
744
|
|
749
|
-
this.info('Subscription acknowledged for '
|
750
|
-
channels.join(', ') + ']');
|
745
|
+
this.info('Subscription acknowledged for ? to ?', this._clientId, channels);
|
751
746
|
|
752
747
|
channels = [].concat(response.subscription);
|
753
748
|
Faye.each(channels, function(channel) {
|
@@ -774,8 +769,7 @@ Faye.Client = Faye.Class({
|
|
774
769
|
channels = [].concat(channels);
|
775
770
|
this._validateChannels(channels);
|
776
771
|
|
777
|
-
this.info('Client
|
778
|
-
channels.join(', ') + ']');
|
772
|
+
this.info('Client ? attempting to unsubscribe from ?', this._clientId, channels);
|
779
773
|
|
780
774
|
this._transport.send({
|
781
775
|
channel: Faye.Channel.UNSUBSCRIBE,
|
@@ -785,12 +779,11 @@ Faye.Client = Faye.Class({
|
|
785
779
|
}, this._verifyClientId(function(response) {
|
786
780
|
if (!response.successful) return;
|
787
781
|
|
788
|
-
this.info('Unsubscription acknowledged for '
|
789
|
-
|
790
|
-
|
782
|
+
this.info('Unsubscription acknowledged for ? from ?', this._clientId, channels);
|
783
|
+
|
791
784
|
channels = [].concat(response.subscription);
|
792
785
|
Faye.each(channels, function(channel) {
|
793
|
-
this._channels.set(channel,
|
786
|
+
this._channels.set(channel, undefined);
|
794
787
|
}, this);
|
795
788
|
}));
|
796
789
|
|
@@ -808,8 +801,7 @@ Faye.Client = Faye.Class({
|
|
808
801
|
|
809
802
|
this._validateChannels([channel]);
|
810
803
|
|
811
|
-
this.info('Client
|
812
|
-
channel + ': ' + Faye.toJSON(data));
|
804
|
+
this.info('Client ? queueing published message to ?: ?', this._clientId, channel, data);
|
813
805
|
|
814
806
|
this._enqueue({
|
815
807
|
channel: channel,
|
@@ -829,12 +821,10 @@ Faye.Client = Faye.Class({
|
|
829
821
|
|
830
822
|
deliverMessages: function(messages) {
|
831
823
|
Faye.each(messages, function(message) {
|
832
|
-
this.info('Client
|
833
|
-
message.channel + ' with ' + Faye.toJSON(message.data));
|
824
|
+
this.info('Client ? calling listeners for ? with ?', this._clientId, message.channel, message.data);
|
834
825
|
|
835
826
|
var channels = this._channels.glob(message.channel);
|
836
827
|
Faye.each(channels, function(callback) {
|
837
|
-
if (!callback) return;
|
838
828
|
callback[0].call(callback[1], message.data);
|
839
829
|
});
|
840
830
|
}, this);
|
@@ -842,12 +832,12 @@ Faye.Client = Faye.Class({
|
|
842
832
|
|
843
833
|
_beginReconnectTimeout: function() {
|
844
834
|
this.addTimeout('reconnect', this._timeout, function() {
|
845
|
-
|
846
|
-
|
835
|
+
this._connectionId = null;
|
836
|
+
this._clientId = null;
|
847
837
|
this._state = this.UNCONNECTED;
|
848
838
|
|
849
|
-
this.info('Server took
|
850
|
-
this._clientId
|
839
|
+
this.info('Server took >?s to reply to connection for ?: attempting to reconnect',
|
840
|
+
this._timeout, this._clientId);
|
851
841
|
|
852
842
|
this.subscribe(this._channels.getKeys());
|
853
843
|
}, this);
|
@@ -935,20 +925,20 @@ Faye.Set = Faye.Class({
|
|
935
925
|
Faye.Server = Faye.Class({
|
936
926
|
initialize: function(options) {
|
937
927
|
this.info('New server created');
|
938
|
-
this._options
|
939
|
-
this._channels
|
940
|
-
this.
|
941
|
-
this._namespace
|
928
|
+
this._options = options || {};
|
929
|
+
this._channels = new Faye.Channel.Tree();
|
930
|
+
this._connections = {};
|
931
|
+
this._namespace = new Faye.Namespace();
|
942
932
|
},
|
943
933
|
|
944
934
|
clientIds: function() {
|
945
935
|
var ids = [];
|
946
|
-
Faye.each(this.
|
936
|
+
Faye.each(this._connections, function(key, value) { ids.push(key) });
|
947
937
|
return ids;
|
948
938
|
},
|
949
939
|
|
950
940
|
process: function(messages, local, callback) {
|
951
|
-
this.debug('Processing messages from '
|
941
|
+
this.debug('Processing messages from ? client', local ? 'LOCAL' : 'REMOTE');
|
952
942
|
|
953
943
|
messages = [].concat(messages);
|
954
944
|
var processed = 0, responses = [];
|
@@ -966,42 +956,41 @@ Faye.Server = Faye.Class({
|
|
966
956
|
flushConnection: function(messages) {
|
967
957
|
messages = [].concat(messages);
|
968
958
|
Faye.each(messages, function(message) {
|
969
|
-
var
|
970
|
-
if (
|
959
|
+
var connection = this._connections[message.clientId];
|
960
|
+
if (connection) connection.flush();
|
971
961
|
}, this);
|
972
962
|
},
|
973
963
|
|
974
964
|
_connection: function(id) {
|
975
|
-
if (this.
|
976
|
-
var
|
977
|
-
|
978
|
-
return this.
|
965
|
+
if (this._connections.hasOwnProperty(id)) return this._connections[id];
|
966
|
+
var connection = new Faye.Connection(id, this._options);
|
967
|
+
connection.on('staleConnection', this._destroyConnection, this);
|
968
|
+
return this._connections[id] = connection;
|
979
969
|
},
|
980
970
|
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
delete this.
|
971
|
+
_destroyConnection: function(connection) {
|
972
|
+
connection.disconnect();
|
973
|
+
connection.stopObserving('staleConnection', this._destroyConnection, this);
|
974
|
+
delete this._connections[connection.id];
|
985
975
|
},
|
986
976
|
|
987
977
|
_handle: function(message, local, callback, scope) {
|
988
|
-
var
|
978
|
+
var channelName = message.channel,
|
989
979
|
response;
|
990
980
|
|
991
981
|
message.__id = Faye.random();
|
992
|
-
Faye.each(this._channels.glob(
|
993
|
-
|
994
|
-
this.info('Publishing message '
|
995
|
-
' from client ' + clientId + ' to ' + c.name);
|
982
|
+
Faye.each(this._channels.glob(channelName), function(channel) {
|
983
|
+
channel.push(message);
|
984
|
+
this.info('Publishing message ? from client ? to ?', message.data, message.clientId, channel.name);
|
996
985
|
}, this);
|
997
986
|
|
998
|
-
if (Faye.Channel.isMeta(
|
999
|
-
response = this[Faye.Channel.parse(
|
987
|
+
if (Faye.Channel.isMeta(channelName)) {
|
988
|
+
response = this[Faye.Channel.parse(channelName)[1]](message, local);
|
1000
989
|
|
1001
990
|
var clientId = response.clientId;
|
1002
991
|
response.advice = response.advice || {};
|
1003
992
|
Faye.extend(response.advice, {
|
1004
|
-
reconnect: this.
|
993
|
+
reconnect: this._connections.hasOwnProperty(clientId) ? 'retry' : 'handshake',
|
1005
994
|
interval: Math.floor(Faye.Connection.prototype.INTERVAL * 1000)
|
1006
995
|
}, false);
|
1007
996
|
|
@@ -1009,16 +998,17 @@ Faye.Server = Faye.Class({
|
|
1009
998
|
response.successful !== true)
|
1010
999
|
return callback.call(scope, response);
|
1011
1000
|
|
1012
|
-
this.info('Accepting connection from '
|
1001
|
+
this.info('Accepting connection from ?', response.clientId);
|
1002
|
+
|
1013
1003
|
return this._connection(response.clientId).connect(function(events) {
|
1014
|
-
this.info('Sending event messages to '
|
1015
|
-
this.debug('Events for '
|
1004
|
+
this.info('Sending event messages to ?', response.clientId);
|
1005
|
+
this.debug('Events for ?: ?', response.clientId, events);
|
1016
1006
|
Faye.each(events, function(e) { delete e.__id });
|
1017
1007
|
callback.call(scope, [response].concat(events));
|
1018
1008
|
}, this);
|
1019
1009
|
}
|
1020
1010
|
|
1021
|
-
if (!message.clientId || Faye.Channel.isService(
|
1011
|
+
if (!message.clientId || Faye.Channel.isService(channelName))
|
1022
1012
|
return callback([]);
|
1023
1013
|
|
1024
1014
|
response = this._makeResponse(message);
|
@@ -1068,7 +1058,7 @@ Faye.Server = Faye.Class({
|
|
1068
1058
|
|
1069
1059
|
var clientId = this._namespace.generate();
|
1070
1060
|
response.clientId = this._connection(clientId).id;
|
1071
|
-
this.info('Accepting handshake from client '
|
1061
|
+
this.info('Accepting handshake from client ?', response.clientId);
|
1072
1062
|
return response;
|
1073
1063
|
},
|
1074
1064
|
|
@@ -1079,11 +1069,11 @@ Faye.Server = Faye.Class({
|
|
1079
1069
|
connect: function(message, local) {
|
1080
1070
|
var response = this._makeResponse(message);
|
1081
1071
|
|
1082
|
-
var clientId
|
1083
|
-
|
1072
|
+
var clientId = message.clientId,
|
1073
|
+
connection = clientId ? this._connections[clientId] : null,
|
1084
1074
|
connectionType = message.connectionType;
|
1085
1075
|
|
1086
|
-
if (!
|
1076
|
+
if (!connection) response.error = Faye.Error.clientUnknown(clientId);
|
1087
1077
|
if (!clientId) response.error = Faye.Error.parameterMissing('clientId');
|
1088
1078
|
if (!connectionType) response.error = Faye.Error.parameterMissing('connectionType');
|
1089
1079
|
|
@@ -1091,7 +1081,7 @@ Faye.Server = Faye.Class({
|
|
1091
1081
|
if (!response.successful) delete response.clientId;
|
1092
1082
|
if (!response.successful) return response;
|
1093
1083
|
|
1094
|
-
response.clientId =
|
1084
|
+
response.clientId = connection.id;
|
1095
1085
|
return response;
|
1096
1086
|
},
|
1097
1087
|
|
@@ -1101,19 +1091,19 @@ Faye.Server = Faye.Class({
|
|
1101
1091
|
disconnect: function(message, local) {
|
1102
1092
|
var response = this._makeResponse(message);
|
1103
1093
|
|
1104
|
-
var clientId
|
1105
|
-
|
1094
|
+
var clientId = message.clientId,
|
1095
|
+
connection = clientId ? this._connections[clientId] : null;
|
1106
1096
|
|
1107
|
-
if (!
|
1108
|
-
if (!clientId)
|
1097
|
+
if (!connection) response.error = Faye.Error.clientUnknown(clientId);
|
1098
|
+
if (!clientId) response.error = Faye.Error.parameterMissing('clientId');
|
1109
1099
|
|
1110
1100
|
response.successful = !response.error;
|
1111
1101
|
if (!response.successful) delete response.clientId;
|
1112
1102
|
if (!response.successful) return response;
|
1113
1103
|
|
1114
|
-
this.
|
1104
|
+
this._destroyConnection(connection);
|
1115
1105
|
|
1116
|
-
this.info('Disconnected client: '
|
1106
|
+
this.info('Disconnected client: ?', clientId);
|
1117
1107
|
response.clientId = clientId;
|
1118
1108
|
return response;
|
1119
1109
|
},
|
@@ -1126,12 +1116,12 @@ Faye.Server = Faye.Class({
|
|
1126
1116
|
var response = this._makeResponse(message);
|
1127
1117
|
|
1128
1118
|
var clientId = message.clientId,
|
1129
|
-
|
1119
|
+
connection = clientId ? this._connections[clientId] : null,
|
1130
1120
|
subscription = message.subscription;
|
1131
1121
|
|
1132
1122
|
subscription = [].concat(subscription);
|
1133
1123
|
|
1134
|
-
if (!
|
1124
|
+
if (!connection) response.error = Faye.Error.clientUnknown(clientId);
|
1135
1125
|
if (!clientId) response.error = Faye.Error.parameterMissing('clientId');
|
1136
1126
|
if (!message.subscription) response.error = Faye.Error.parameterMissing('subscription');
|
1137
1127
|
|
@@ -1145,8 +1135,8 @@ Faye.Server = Faye.Class({
|
|
1145
1135
|
if (response.error) return;
|
1146
1136
|
channel = this._channels.findOrCreate(channel);
|
1147
1137
|
|
1148
|
-
this.info('Subscribing client
|
1149
|
-
|
1138
|
+
this.info('Subscribing client ? to ?', clientId, channel.name);
|
1139
|
+
connection.subscribe(channel);
|
1150
1140
|
}, this);
|
1151
1141
|
|
1152
1142
|
response.successful = !response.error;
|
@@ -1161,15 +1151,17 @@ Faye.Server = Faye.Class({
|
|
1161
1151
|
var response = this._makeResponse(message);
|
1162
1152
|
|
1163
1153
|
var clientId = message.clientId,
|
1164
|
-
|
1154
|
+
connection = clientId ? this._connections[clientId] : null,
|
1165
1155
|
subscription = message.subscription;
|
1166
1156
|
|
1167
1157
|
subscription = [].concat(subscription);
|
1168
1158
|
|
1169
|
-
if (!
|
1159
|
+
if (!connection) response.error = Faye.Error.clientUnknown(clientId);
|
1170
1160
|
if (!clientId) response.error = Faye.Error.parameterMissing('clientId');
|
1171
1161
|
if (!message.subscription) response.error = Faye.Error.parameterMissing('subscription');
|
1172
1162
|
|
1163
|
+
response.subscription = subscription;
|
1164
|
+
|
1173
1165
|
Faye.each(subscription, function(channel) {
|
1174
1166
|
if (response.error) return;
|
1175
1167
|
|
@@ -1179,8 +1171,8 @@ Faye.Server = Faye.Class({
|
|
1179
1171
|
channel = this._channels.get(channel);
|
1180
1172
|
if (!channel) return;
|
1181
1173
|
|
1182
|
-
this.info('Unsubscribing client
|
1183
|
-
|
1174
|
+
this.info('Unsubscribing client ? from ?', clientId, channel.name);
|
1175
|
+
connection.unsubscribe(channel);
|
1184
1176
|
}, this);
|
1185
1177
|
|
1186
1178
|
response.successful = !response.error;
|
@@ -1265,7 +1257,7 @@ Faye.Connection = Faye.Class({
|
|
1265
1257
|
this._connected = false;
|
1266
1258
|
|
1267
1259
|
this.addTimeout('deletion', 10 * this.INTERVAL, function() {
|
1268
|
-
this.
|
1260
|
+
this.trigger('staleConnection', this);
|
1269
1261
|
}, this);
|
1270
1262
|
}
|
1271
1263
|
});
|
@@ -1394,9 +1386,9 @@ Faye.NodeAdapter = Faye.Class({
|
|
1394
1386
|
|
1395
1387
|
if (/\.js$/.test(requestUrl.pathname)) {
|
1396
1388
|
fs.readFile(this.SCRIPT_PATH, function(err, content) {
|
1397
|
-
response.
|
1389
|
+
response.writeHead(200, self.TYPE_SCRIPT);
|
1398
1390
|
response.write(content);
|
1399
|
-
response.
|
1391
|
+
response.end();
|
1400
1392
|
});
|
1401
1393
|
|
1402
1394
|
} else {
|
@@ -1425,14 +1417,14 @@ Faye.NodeAdapter = Faye.Class({
|
|
1425
1417
|
this._server.process(message, false, function(replies) {
|
1426
1418
|
var body = JSON.stringify(replies);
|
1427
1419
|
if (isGet) body = jsonp + '(' + body + ');';
|
1428
|
-
response.
|
1420
|
+
response.writeHead(200, type);
|
1429
1421
|
response.write(body);
|
1430
|
-
response.
|
1422
|
+
response.end();
|
1431
1423
|
});
|
1432
1424
|
} catch (e) {
|
1433
|
-
response.
|
1425
|
+
response.writeHead(400, this.TYPE_TEXT);
|
1434
1426
|
response.write('Bad request');
|
1435
|
-
response.
|
1427
|
+
response.end();
|
1436
1428
|
}
|
1437
1429
|
}
|
1438
1430
|
});
|
@@ -1451,7 +1443,7 @@ Faye.NodeHttpTransport = Faye.Class(Faye.Transport, {
|
|
1451
1443
|
callback.call(scope, JSON.parse(data));
|
1452
1444
|
});
|
1453
1445
|
});
|
1454
|
-
request.
|
1446
|
+
request.end();
|
1455
1447
|
},
|
1456
1448
|
|
1457
1449
|
createRequestForMessage: function(message) {
|
@@ -1459,6 +1451,8 @@ Faye.NodeHttpTransport = Faye.Class(Faye.Transport, {
|
|
1459
1451
|
uri = url.parse(this._endpoint),
|
1460
1452
|
client = http.createClient(uri.port, uri.hostname);
|
1461
1453
|
|
1454
|
+
client.addListener('error', function() { /* catch ECONNREFUSED */ });
|
1455
|
+
|
1462
1456
|
if (parseInt(uri.port) === 443) client.setSecure('X509_PEM');
|
1463
1457
|
|
1464
1458
|
var request = client.request('POST', uri.pathname, {
|