faye-rails 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: af85b994256de6b3e8b26e98a1cc6f613f0ecd8f
4
+ data.tar.gz: 156299d6db10678a7b47dcc1fb59b638e486e015
5
+ SHA512:
6
+ metadata.gz: b8f832cec7453e5453ae316b8eba2c99534ab9e1c538418cdd6cc45223f2e2c8eb865e4a953506d55d71cc2d7a641f8740cb6345e0a42be1cc6bac0e2bab9bed
7
+ data.tar.gz: 96b58a15968578aedb0a78a6374e8f3b9f25000bc681aafb177fe27cff9af8a208d825a57221fcbd29d048864619ae36b11f50017958bfe2a6b6009c7e89159f
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
- # faye-rails [![Build Status](https://secure.travis-ci.org/jamesotron/faye-rails.png)](http://travis-ci.org/jamesotron/faye-rails)
1
+ # faye-rails
2
2
 
3
3
  faye-rails is a Ruby gem which handles embedding Faye's rack-based server into the rails stack and providing it with access to controllers and views based on bindings and observers.
4
4
 
5
5
  [![Build Status](https://travis-ci.org/jamesotron/faye-rails.png?branch=master)](https://travis-ci.org/jamesotron/faye-rails)
6
6
  [![Dependency Status](https://gemnasium.com/jamesotron/faye-rails.png)](https://gemnasium.com/jamesotron/faye-rails)
7
- [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jamesotron/faye-rails)
7
+ [![Code Climate](https://codeclimate.com/github/jamesotron/faye-rails.png)](https://codeclimate.com/github/jamesotron/faye-rails)
8
+
9
+ A *very* small demonstration app is available for your perusal [on Heroku](http://faye-rails-demo.herokuapp.com/). The source is [here on Github](https://github.com/jamesotron/faye-rails-demo).
8
10
 
9
11
  # Embedded server
10
12
 
@@ -33,8 +33,8 @@ module FayeRails
33
33
  #
34
34
  # :default can be set to :allow, :drop or :block.
35
35
  # if :drop is chosen then messages to unknown channels
36
- # will be silently dropped, whereas if you choose
37
- # :block then the message will be returned with the
36
+ # will be silently dropped, whereas if you choose
37
+ # :block then the message will be returned with the
38
38
  # error "Permission denied."
39
39
  def map(opts)
40
40
  if opts.is_a? Hash
@@ -64,7 +64,7 @@ module FayeRails
64
64
  add_extension(DebugMessagesExtension.new)
65
65
  end
66
66
 
67
- private
67
+ private
68
68
 
69
69
  def routing_extension
70
70
  if @routing_extension
@@ -75,9 +75,9 @@ module FayeRails
75
75
  @routing_extension
76
76
  end
77
77
  end
78
-
78
+
79
79
  class DebugMessagesExtension
80
-
80
+
81
81
  def debug(*args)
82
82
  if defined? ::Rails
83
83
  Rails.logger.debug *args
@@ -97,7 +97,7 @@ module FayeRails
97
97
  end
98
98
  end
99
99
 
100
- class RoutingExtension
100
+ class RoutingExtension
101
101
 
102
102
  def initialize
103
103
  @default = :allow
@@ -1,3 +1,3 @@
1
1
  module FayeRails
2
- VERSION = "1.0.6"
2
+ VERSION = "1.0.7"
3
3
  end
@@ -1,2 +1,2 @@
1
- var Faye=(typeof Faye==='object')?Faye:{};if(typeof window!=='undefined')window.Faye=Faye;Faye.extend=function(a,b,d){if(!b)return a;for(var f in b){if(!b.hasOwnProperty(f))continue;if(a.hasOwnProperty(f)&&d===false)continue;if(a[f]!==b[f])a[f]=b[f]}return a};Faye.extend(Faye,{VERSION:'0.8.8',BAYEUX_VERSION:'1.0',ID_LENGTH:160,JSONP_CALLBACK:'jsonpcallback',CONNECTION_TYPES:['long-polling','cross-origin-long-polling','callback-polling','websocket','eventsource','in-process'],MANDATORY_CONNECTION_TYPES:['long-polling','callback-polling','in-process'],ENV:(function(){return this})(),random:function(a){a=a||this.ID_LENGTH;if(a>32){var b=Math.ceil(a/32),d='';while(b--)d+=this.random(32);var f=d.split(''),g='';while(f.length>0)g+=f.pop();return g}var h=Math.pow(2,a)-1,i=h.toString(36).length,d=Math.floor(Math.random()*h).toString(36);while(d.length<i)d='0'+d;return d},clientIdFromMessages:function(a){var b=[].concat(a)[0];return b&&b.clientId},copyObject:function(a){var b,d,f;if(a instanceof Array){b=[];d=a.length;while(d--)b[d]=Faye.copyObject(a[d]);return b}else if(typeof a==='object'){b=(a===null)?null:{};for(f in a)b[f]=Faye.copyObject(a[f]);return b}else{return a}},commonElement:function(a,b){for(var d=0,f=a.length;d<f;d++){if(this.indexOf(b,a[d])!==-1)return a[d]}return null},indexOf:function(a,b){if(a.indexOf)return a.indexOf(b);for(var d=0,f=a.length;d<f;d++){if(a[d]===b)return d}return-1},map:function(a,b,d){if(a.map)return a.map(b,d);var f=[];if(a instanceof Array){for(var g=0,h=a.length;g<h;g++){f.push(b.call(d||null,a[g],g))}}else{for(var i in a){if(!a.hasOwnProperty(i))continue;f.push(b.call(d||null,i,a[i]))}}return f},filter:function(a,b,d){var f=[];for(var g=0,h=a.length;g<h;g++){if(b.call(d||null,a[g],g))f.push(a[g])}return f},asyncEach:function(a,b,d,f){var g=a.length,h=-1,i=0,k=false;var j=function(){i-=1;h+=1;if(h===g)return d&&d.call(f);b(a[h],n)};var l=function(){if(k)return;k=true;while(i>0)j();k=false};var n=function(){i+=1;l()};n()},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)},logger:function(a){if(typeof console!=='undefined')console.log(a)},timestamp:function(){var b=new Date(),d=b.getFullYear(),f=b.getMonth()+1,g=b.getDate(),h=b.getHours(),i=b.getMinutes(),k=b.getSeconds();var j=function(a){return a<10?'0'+a:String(a)};return j(d)+'-'+j(f)+'-'+j(g)+' '+j(h)+':'+j(i)+':'+j(k)}});Faye.Class=function(a,b){if(typeof a!=='function'){b=a;a=Object}var d=function(){if(!this.initialize)return this;return this.initialize.apply(this,arguments)||this};var f=function(){};f.prototype=a.prototype;d.prototype=new f();Faye.extend(d.prototype,b);return d};Faye.Namespace=Faye.Class({initialize:function(){this._e={}},exists:function(a){return this._e.hasOwnProperty(a)},generate:function(){var a=Faye.random();while(this._e.hasOwnProperty(a))a=Faye.random();return this._e[a]=a},release:function(a){delete this._e[a]}});Faye.Error=Faye.Class({initialize:function(a,b,d){this.code=a;this.params=Array.prototype.slice.call(b);this.message=d},toString:function(){return this.code+':'+this.params.join(',')+':'+this.message}});Faye.Error.parse=function(a){a=a||'';if(!Faye.Grammar.ERROR.test(a))return new this(null,[],a);var b=a.split(':'),d=parseInt(b[0]),f=b[1].split(','),a=b[2];return new this(d,f,a)};Faye.Error.versionMismatch=function(){return new this(300,arguments,"Version mismatch").toString()};Faye.Error.conntypeMismatch=function(){return new this(301,arguments,"Connection types not supported").toString()};Faye.Error.extMismatch=function(){return new this(302,arguments,"Extension mismatch").toString()};Faye.Error.badRequest=function(){return new this(400,arguments,"Bad request").toString()};Faye.Error.clientUnknown=function(){return new this(401,arguments,"Unknown client").toString()};Faye.Error.parameterMissing=function(){return new this(402,arguments,"Missing required parameter").toString()};Faye.Error.channelForbidden=function(){return new this(403,arguments,"Forbidden channel").toString()};Faye.Error.channelUnknown=function(){return new this(404,arguments,"Unknown channel").toString()};Faye.Error.channelInvalid=function(){return new this(405,arguments,"Invalid channel").toString()};Faye.Error.extUnknown=function(){return new this(406,arguments,"Unknown extension").toString()};Faye.Error.publishFailed=function(){return new this(407,arguments,"Failed to publish").toString()};Faye.Error.serverError=function(){return new this(500,arguments,"Internal server error").toString()};Faye.Deferrable={callback:function(a,b){if(!a)return;if(this._w==='succeeded')return a.apply(b,this._j);this._k=this._k||[];this._k.push([a,b])},timeout:function(a,b){var d=this;var f=Faye.ENV.setTimeout(function(){d.setDeferredStatus('failed',b)},a*1000);this._x=f},errback:function(a,b){if(!a)return;if(this._w==='failed')return a.apply(b,this._j);this._l=this._l||[];this._l.push([a,b])},setDeferredStatus:function(){if(this._x)Faye.ENV.clearTimeout(this._x);var a=Array.prototype.slice.call(arguments),b=a.shift(),d;this._w=b;this._j=a;if(b==='succeeded')d=this._k;else if(b==='failed')d=this._l;if(!d)return;var f;while(f=d.shift())f[0].apply(f[1],this._j)}};Faye.Publisher={countListeners:function(a){if(!this._3||!this._3[a])return 0;return this._3[a].length},bind:function(a,b,d){this._3=this._3||{};var f=this._3[a]=this._3[a]||[];f.push([b,d])},unbind:function(a,b,d){if(!this._3||!this._3[a])return;if(!b){delete this._3[a];return}var f=this._3[a],g=f.length;while(g--){if(b!==f[g][0])continue;if(d&&f[g][1]!==d)continue;f.splice(g,1)}},trigger:function(){var a=Array.prototype.slice.call(arguments),b=a.shift();if(!this._3||!this._3[b])return;var d=this._3[b].slice(),f;for(var g=0,h=d.length;g<h;g++){f=d[g];f[0].apply(f[1],a)}}};Faye.Timeouts={addTimeout:function(a,b,d,f){this._5=this._5||{};if(this._5.hasOwnProperty(a))return;var g=this;this._5[a]=Faye.ENV.setTimeout(function(){delete g._5[a];d.call(f)},1000*b)},removeTimeout:function(a){this._5=this._5||{};var b=this._5[a];if(!b)return;clearTimeout(b);delete this._5[a]}};Faye.Logging={LOG_LEVELS:{error:3,warn:2,info:1,debug:0},logLevel:'error',log:function(a,b){if(!Faye.logger)return;var d=Faye.Logging.LOG_LEVELS;if(d[Faye.Logging.logLevel]>d[b])return;var a=Array.prototype.slice.apply(a),f=' ['+b.toUpperCase()+'] [Faye',g=this.className,h=a.shift().replace(/\?/g,function(){try{return Faye.toJSON(a.shift())}catch(e){return'[Object]'}});for(var i in Faye){if(g)continue;if(typeof Faye[i]!=='function')continue;if(this instanceof Faye[i])g=i}if(g)f+='.'+g;f+='] ';Faye.logger(Faye.timestamp()+f+h)}};(function(){for(var d in Faye.Logging.LOG_LEVELS)(function(a,b){Faye.Logging[a]=function(){this.log(arguments,a)}})(d,Faye.Logging.LOG_LEVELS[d])})();Faye.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])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)$/};Faye.Extensible={addExtension:function(a){this._6=this._6||[];this._6.push(a);if(a.added)a.added(this)},removeExtension:function(a){if(!this._6)return;var b=this._6.length;while(b--){if(this._6[b]!==a)continue;this._6.splice(b,1);if(a.removed)a.removed(this)}},pipeThroughExtensions:function(d,f,g,h){this.debug('Passing through ? extensions: ?',d,f);if(!this._6)return g.call(h,f);var i=this._6.slice();var k=function(a){if(!a)return g.call(h,a);var b=i.shift();if(!b)return g.call(h,a);if(b[d])b[d](a,k);else k(a)};k(f)}};Faye.extend(Faye.Extensible,Faye.Logging);Faye.Channel=Faye.Class({initialize:function(a){this.id=this.name=a},push:function(a){this.trigger('message',a)},isUnused:function(){return this.countListeners('message')===0}});Faye.extend(Faye.Channel.prototype,Faye.Publisher);Faye.extend(Faye.Channel,{HANDSHAKE:'/meta/handshake',CONNECT:'/meta/connect',SUBSCRIBE:'/meta/subscribe',UNSUBSCRIBE:'/meta/unsubscribe',DISCONNECT:'/meta/disconnect',META:'meta',SERVICE:'service',expand:function(a){var b=this.parse(a),d=['/**',a];var f=b.slice();f[f.length-1]='*';d.push(this.unparse(f));for(var g=1,h=b.length;g<h;g++){f=b.slice(0,g);f.push('**');d.push(this.unparse(f))}return d},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)},unparse:function(a){return'/'+a.join('/')},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)},Set:Faye.Class({initialize:function(){this._2={}},getKeys:function(){var a=[];for(var b in this._2)a.push(b);return a},remove:function(a){delete this._2[a]},hasSubscription:function(a){return this._2.hasOwnProperty(a)},subscribe:function(a,b,d){if(!b)return;var f;for(var g=0,h=a.length;g<h;g++){f=a[g];var i=this._2[f]=this._2[f]||new Faye.Channel(f);i.bind('message',b,d)}},unsubscribe:function(a,b,d){var f=this._2[a];if(!f)return false;f.unbind('message',b,d);if(f.isUnused()){this.remove(a);return true}else{return false}},distributeMessage:function(a){var b=Faye.Channel.expand(a.channel);for(var d=0,f=b.length;d<f;d++){var g=this._2[b[d]];if(g)g.trigger('message',a.data)}}})});Faye.Publication=Faye.Class(Faye.Deferrable);Faye.Subscription=Faye.Class({initialize:function(a,b,d,f){this._7=a;this._2=b;this._m=d;this._n=f;this._y=false},cancel:function(){if(this._y)return;this._7.unsubscribe(this._2,this._m,this._n);this._y=true},unsubscribe:function(){this.cancel()}});Faye.extend(Faye.Subscription.prototype,Faye.Deferrable);Faye.Client=Faye.Class({UNCONNECTED:1,CONNECTING:2,CONNECTED:3,DISCONNECTED:4,HANDSHAKE:'handshake',RETRY:'retry',NONE:'none',CONNECTION_TIMEOUT:60.0,DEFAULT_RETRY:5.0,DEFAULT_ENDPOINT:'/bayeux',INTERVAL:0.0,initialize:function(a,b){this.info('New client created for ?',a);this._f=b||{};this.endpoint=a||this.DEFAULT_ENDPOINT;this.endpoints=this._f.endpoints||{};this.transports={};this._E=Faye.CookieJar&&new Faye.CookieJar();this._z={};this._o=[];this.retry=this._f.retry||this.DEFAULT_RETRY;this._A(Faye.MANDATORY_CONNECTION_TYPES);this._1=this.UNCONNECTED;this._2=new Faye.Channel.Set();this._g=0;this._p={};this._8={reconnect:this.RETRY,interval:1000*(this._f.interval||this.INTERVAL),timeout:1000*(this._f.timeout||this.CONNECTION_TIMEOUT)};if(Faye.Event)Faye.Event.on(Faye.ENV,'beforeunload',function(){if(Faye.indexOf(this._o,'autodisconnect')<0)this.disconnect()},this)},disable:function(a){this._o.push(a)},setHeader:function(a,b){this._z[a]=b},getClientId:function(){return this._0},getState:function(){switch(this._1){case this.UNCONNECTED:return'UNCONNECTED';case this.CONNECTING:return'CONNECTING';case this.CONNECTED:return'CONNECTED';case this.DISCONNECTED:return'DISCONNECTED'}},handshake:function(f,g){if(this._8.reconnect===this.NONE)return;if(this._1!==this.UNCONNECTED)return;this._1=this.CONNECTING;var h=this;this.info('Initiating handshake with ?',this.endpoint);this._9({channel:Faye.Channel.HANDSHAKE,version:Faye.BAYEUX_VERSION,supportedConnectionTypes:[this._a.connectionType]},function(b){if(b.successful){this._1=this.CONNECTED;this._0=b.clientId;var d=Faye.filter(b.supportedConnectionTypes,function(a){return Faye.indexOf(this._o,a)<0},this);this._A(d);this.info('Handshake successful: ?',this._0);this.subscribe(this._2.getKeys(),true);if(f)f.call(g)}else{this.info('Handshake unsuccessful');Faye.ENV.setTimeout(function(){h.handshake(f,g)},this._8.interval);this._1=this.UNCONNECTED}},this)},connect:function(a,b){if(this._8.reconnect===this.NONE)return;if(this._1===this.DISCONNECTED)return;if(this._1===this.UNCONNECTED)return this.handshake(function(){this.connect(a,b)},this);this.callback(a,b);if(this._1!==this.CONNECTED)return;this.info('Calling deferred actions for ?',this._0);this.setDeferredStatus('succeeded');this.setDeferredStatus('deferred');if(this._q)return;this._q=true;this.info('Initiating connection for ?',this._0);this._9({channel:Faye.Channel.CONNECT,clientId:this._0,connectionType:this._a.connectionType},this._B,this)},disconnect:function(){if(this._1!==this.CONNECTED)return;this._1=this.DISCONNECTED;this.info('Disconnecting ?',this._0);this._9({channel:Faye.Channel.DISCONNECT,clientId:this._0},function(a){if(a.successful)this._a.close()},this);this.info('Clearing channel listeners for ?',this._0);this._2=new Faye.Channel.Set()},subscribe:function(d,f,g){if(d instanceof Array)return Faye.map(d,function(c){return this.subscribe(c,f,g)},this);var h=new Faye.Subscription(this,d,f,g),i=(f===true),k=this._2.hasSubscription(d);if(k&&!i){this._2.subscribe([d],f,g);h.setDeferredStatus('succeeded');return h}this.connect(function(){this.info('Client ? attempting to subscribe to ?',this._0,d);if(!i)this._2.subscribe([d],f,g);this._9({channel:Faye.Channel.SUBSCRIBE,clientId:this._0,subscription:d},function(a){if(!a.successful){h.setDeferredStatus('failed',Faye.Error.parse(a.error));return this._2.unsubscribe(d,f,g)}var b=[].concat(a.subscription);this.info('Subscription acknowledged for ? to ?',this._0,b);h.setDeferredStatus('succeeded')},this)},this);return h},unsubscribe:function(d,f,g){if(d instanceof Array)return Faye.map(d,function(c){return this.unsubscribe(c,f,g)},this);var h=this._2.unsubscribe(d,f,g);if(!h)return;this.connect(function(){this.info('Client ? attempting to unsubscribe from ?',this._0,d);this._9({channel:Faye.Channel.UNSUBSCRIBE,clientId:this._0,subscription:d},function(a){if(!a.successful)return;var b=[].concat(a.subscription);this.info('Unsubscription acknowledged for ? from ?',this._0,b)},this)},this)},publish:function(b,d){var f=new Faye.Publication();this.connect(function(){this.info('Client ? queueing published message to ?: ?',this._0,b,d);this._9({channel:b,data:d,clientId:this._0},function(a){if(a.successful)f.setDeferredStatus('succeeded');else f.setDeferredStatus('failed',Faye.Error.parse(a.error))},this)},this);return f},receiveMessage:function(d){this.pipeThroughExtensions('incoming',d,function(a){if(!a)return;if(a.advice)this._F(a.advice);this._G(a);if(a.successful===undefined)return;var b=this._p[a.id];if(!b)return;delete this._p[a.id];b[0].call(b[1],a)},this)},_A:function(b){Faye.Transport.get(this,b,function(a){this.debug('Selected ? transport for ?',a.connectionType,a.endpoint);this._a=a;this._a.cookies=this._E;this._a.headers=this._z;a.bind('down',function(){if(this._b!==undefined&&!this._b)return;this._b=false;this.trigger('transport:down')},this);a.bind('up',function(){if(this._b!==undefined&&this._b)return;this._b=true;this.trigger('transport:up')},this)},this)},_9:function(b,d,f){b.id=this._H();if(d)this._p[b.id]=[d,f];this.pipeThroughExtensions('outgoing',b,function(a){if(!a)return;this._a.send(a,this._8.timeout/1000)},this)},_H:function(){this._g+=1;if(this._g>=Math.pow(2,32))this._g=0;return this._g.toString(36)},_F:function(a){Faye.extend(this._8,a);if(this._8.reconnect===this.HANDSHAKE&&this._1!==this.DISCONNECTED){this._1=this.UNCONNECTED;this._0=null;this._B()}},_G:function(a){if(!a.channel||a.data===undefined)return;this.info('Client ? calling listeners for ? with ?',this._0,a.channel,a.data);this._2.distributeMessage(a)},_I:function(){if(!this._q)return;this._q=null;this.info('Closed connection for ?',this._0)},_B:function(){this._I();var a=this;Faye.ENV.setTimeout(function(){a.connect()},this._8.interval)}});Faye.extend(Faye.Client.prototype,Faye.Deferrable);Faye.extend(Faye.Client.prototype,Faye.Publisher);Faye.extend(Faye.Client.prototype,Faye.Logging);Faye.extend(Faye.Client.prototype,Faye.Extensible);Faye.Transport=Faye.extend(Faye.Class({MAX_DELAY:0.0,batching:true,initialize:function(a,b){this._7=a;this.endpoint=b;this._c=[]},close:function(){},send:function(a,b){this.debug('Client ? sending message to ?: ?',this._7._0,this.endpoint,a);if(!this.batching)return this.request([a],b);this._c.push(a);this._J=b;if(a.channel===Faye.Channel.HANDSHAKE)return this.addTimeout('publish',0.01,this.flush,this);if(a.channel===Faye.Channel.CONNECT)this._r=a;if(this.shouldFlush&&this.shouldFlush(this._c))return this.flush();this.addTimeout('publish',this.MAX_DELAY,this.flush,this)},flush:function(){this.removeTimeout('publish');if(this._c.length>1&&this._r)this._r.advice={timeout:0};this.request(this._c,this._J);this._r=null;this._c=[]},receive:function(a){this.debug('Client ? received from ?: ?',this._7._0,this.endpoint,a);for(var b=0,d=a.length;b<d;b++){this._7.receiveMessage(a[b])}},retry:function(a,b){var d=false,f=this._7.retry*1000,g=this;return function(){if(d)return;d=true;Faye.ENV.setTimeout(function(){g.request(a,b)},f)}}}),{MAX_URL_LENGTH:2048,get:function(k,j,l,n){var o=k.endpoint;if(j===undefined)j=this.supportedConnectionTypes();Faye.asyncEach(this._s,function(d,f){var g=d[0],h=d[1],i=k.endpoints[g]||o;if(Faye.indexOf(j,g)<0){h.isUsable(k,i,function(){});return f()}h.isUsable(k,i,function(a){if(!a)return f();var b=h.hasOwnProperty('create')?h.create(k,i):new h(k,i);l.call(n,b)})},function(){throw new Error('Could not find a usable connection type for '+o);})},register:function(a,b){this._s.push([a,b]);b.prototype.connectionType=a},_s:[],supportedConnectionTypes:function(){return Faye.map(this._s,function(a){return a[0]})}});Faye.extend(Faye.Transport.prototype,Faye.Logging);Faye.extend(Faye.Transport.prototype,Faye.Publisher);Faye.extend(Faye.Transport.prototype,Faye.Timeouts);Faye.Event={_h:[],on:function(a,b,d,f){var g=function(){d.call(f)};if(a.addEventListener)a.addEventListener(b,g,false);else a.attachEvent('on'+b,g);this._h.push({_i:a,_t:b,_m:d,_n:f,_C:g})},detach:function(a,b,d,f){var g=this._h.length,h;while(g--){h=this._h[g];if((a&&a!==h._i)||(b&&b!==h._t)||(d&&d!==h._m)||(f&&f!==h._n))continue;if(h._i.removeEventListener)h._i.removeEventListener(h._t,h._C,false);else h._i.detachEvent('on'+h._t,h._C);this._h.splice(g,1);h=null}}};Faye.Event.on(Faye.ENV,'unload',Faye.Event.detach,Faye.Event);Faye.URI=Faye.extend(Faye.Class({queryString:function(){var a=[];for(var b in this.params){if(!this.params.hasOwnProperty(b))continue;a.push(encodeURIComponent(b)+'='+encodeURIComponent(this.params[b]))}return a.join('&')},isSameOrigin: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(){var a=this.queryString();return this.protocol+'//'+this.hostname+(this.port?':'+this.port:'')+this.pathname+(a?'?'+a:'')+this.hash}}),{parse:function(g,h){if(typeof g!=='string')return g;var i=new this(),k;var j=function(b,d,f){g=g.replace(d,function(a){i[b]=a;return''});if(i[b]===undefined)i[b]=f?Faye.ENV.location[b]:''};j('protocol',/^https?\:/,true);j('host',/^\/\/[^\/]+/,true);if(!/^\//.test(g))g=Faye.ENV.location.pathname.replace(/[^\/]*$/,'')+g;j('pathname',/^\/[^\?#]*/);j('search',/^\?[^#]*/);j('hash',/^#.*/);if(/^\/\//.test(i.host)){i.host=i.host.substr(2);k=i.host.split(':');i.hostname=k[0];i.port=k[1]||''}else{i.hostname=Faye.ENV.location.hostname;i.port=Faye.ENV.location.port}var l=i.search.replace(/^\?/,''),n=l?l.split('&'):[],o=n.length,m={};while(o--){k=n[o].split('=');m[decodeURIComponent(k[0]||'')]=decodeURIComponent(k[1]||'')}if(typeof h==='object')Faye.extend(m,h);i.params=m;return i}});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,m,q,t={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},p;function s(d){o.lastIndex=0;return o.test(d)?'"'+d.replace(o,function(a){var b=t[a];return typeof b==='string'?b:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+d+'"'}function r(a,b){var d,f,g,h,i=m,k,j=b[a];if(j&&typeof j==='object'&&typeof j.toJSON==='function'){j=j.toJSON(a)}if(typeof p==='function'){j=p.call(b,a,j)}switch(typeof j){case'string':return s(j);case'number':return isFinite(j)?String(j):'null';case'boolean':case'null':return String(j);case'object':if(!j){return'null'}m+=q;k=[];if(Object.prototype.toString.apply(j)==='[object Array]'){h=j.length;for(d=0;d<h;d+=1){k[d]=r(d,j)||'null'}g=k.length===0?'[]':m?'[\n'+m+k.join(',\n'+m)+'\n'+i+']':'['+k.join(',')+']';m=i;return g}if(p&&typeof p==='object'){h=p.length;for(d=0;d<h;d+=1){f=p[d];if(typeof f==='string'){g=r(f,j);if(g){k.push(s(f)+(m?': ':':')+g)}}}}else{for(f in j){if(Object.hasOwnProperty.call(j,f)){g=r(f,j);if(g){k.push(s(f)+(m?': ':':')+g)}}}}g=k.length===0?'{}':m?'{\n'+m+k.join(',\n'+m)+'\n'+i+'}':'{'+k.join(',')+'}';m=i;return g}}Faye.stringify=function(a,b,d){var f;m='';q='';if(typeof d==='number'){for(f=0;f<d;f+=1){q+=' '}}else if(typeof d==='string'){q=d}p=b;if(b&&typeof b!=='function'&&(typeof b!=='object'||typeof b.length!=='number')){throw new Error('JSON.stringify');}return r('',{'':a})};if(typeof JSON.stringify!=='function'){JSON.stringify=Faye.stringify}if(typeof JSON.parse!=='function'){JSON.parse=function(h,i){var k;function j(a,b){var d,f,g=a[b];if(g&&typeof g==='object'){for(d in g){if(Object.hasOwnProperty.call(g,d)){f=j(g,d);if(f!==undefined){g[d]=f}else{delete g[d]}}}}return i.call(a,b,g)}n.lastIndex=0;if(n.test(h)){h=h.replace(n,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(h.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){k=eval('('+h+')');return typeof i==='function'?j({'':k},''):k}throw new SyntaxError('JSON.parse');}}}());Faye.Transport.WebSocket=Faye.extend(Faye.Class(Faye.Transport,{UNCONNECTED:1,CONNECTING:2,CONNECTED:3,batching:false,isUsable:function(a,b){this.callback(function(){a.call(b,true)});this.errback(function(){a.call(b,false)});this.connect()},request:function(b,d){if(b.length===0)return;this._d=this._d||{};for(var f=0,g=b.length;f<g;f++){this._d[b[f].id]=b[f]}this.callback(function(a){a.send(Faye.toJSON(b))});this.connect()},close:function(){if(this._D)return;this._D=true;if(this._4)this._4.close()},connect:function(){if(Faye.Transport.WebSocket._K)return;if(this._D)return;this._1=this._1||this.UNCONNECTED;if(this._1!==this.UNCONNECTED)return;this._1=this.CONNECTING;var g=Faye.Transport.WebSocket.getClass();if(!g)return this.setDeferredStatus('failed');this._4=new g(Faye.Transport.WebSocket.getSocketUrl(this.endpoint));var h=this;this._4.onopen=function(){h._1=h.CONNECTED;h._u=true;h.setDeferredStatus('succeeded',h._4);h.trigger('up')};this._4.onmessage=function(a){var b=[].concat(JSON.parse(a.data));for(var d=0,f=b.length;d<f;d++){delete h._d[b[d].id]}h.receive(b)};this._4.onclose=function(){var a=(h._1===h.CONNECTED);h.setDeferredStatus('deferred');h._1=h.UNCONNECTED;delete h._4;if(a)return h.resend();if(!h._u)return h.setDeferredStatus('failed');var b=h._7.retry*1000;Faye.ENV.setTimeout(function(){h.connect()},b);h.trigger('down')}},resend:function(){if(!this._d)return;var d=Faye.map(this._d,function(a,b){return b});this.request(d)}}),{getSocketUrl:function(a){if(Faye.URI)a=Faye.URI.parse(a).toURL();return a.replace(/^http(s?):/ig,'ws$1:')},getClass:function(){return(Faye.WebSocket&&Faye.WebSocket.Client)||Faye.ENV.WebSocket||Faye.ENV.MozWebSocket},isUsable:function(a,b,d,f){this.create(a,b).isUsable(d,f)},create:function(a,b){var d=a.transports.websocket=a.transports.websocket||{};d[b]=d[b]||new this(a,b);return d[b]}});Faye.extend(Faye.Transport.WebSocket.prototype,Faye.Deferrable);Faye.Transport.register('websocket',Faye.Transport.WebSocket);if(Faye.Event)Faye.Event.on(Faye.ENV,'beforeunload',function(){Faye.Transport.WebSocket._K=true});Faye.Transport.EventSource=Faye.extend(Faye.Class(Faye.Transport,{initialize:function(b,d){Faye.Transport.prototype.initialize.call(this,b,d);if(!Faye.ENV.EventSource)return this.setDeferredStatus('failed');this._L=new Faye.Transport.XHR(b,d);var f=new EventSource(d+'/'+b.getClientId()),g=this;f.onopen=function(){g._u=true;g.setDeferredStatus('succeeded');g.trigger('up')};f.onerror=function(){if(g._u){g.trigger('down')}else{g.setDeferredStatus('failed');f.close()}};f.onmessage=function(a){g.receive(JSON.parse(a.data));g.trigger('up')};this._4=f},isUsable:function(a,b){this.callback(function(){a.call(b,true)});this.errback(function(){a.call(b,false)})},request:function(a,b){this._L.request(a,b)},close:function(){this._4.close()}}),{isUsable:function(b,d,f,g){var h=b.getClientId();if(!h)return f.call(g,false);Faye.Transport.XHR.isUsable(b,d,function(a){if(!a)return f.call(g,false);this.create(b,d).isUsable(f,g)},this)},create:function(a,b){var d=a.transports.eventsource=a.transports.eventsource||{};d[b]=d[b]||new this(a,b);return d[b]}});Faye.extend(Faye.Transport.EventSource.prototype,Faye.Deferrable);Faye.Transport.register('eventsource',Faye.Transport.EventSource);Faye.Transport.XHR=Faye.extend(Faye.Class(Faye.Transport,{request:function(f,g){var h=this.retry(f,g),i=Faye.URI.parse(this.endpoint).pathname,k=this,j=Faye.ENV.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();j.open('POST',i,true);j.setRequestHeader('Content-Type','application/json');j.setRequestHeader('Pragma','no-cache');j.setRequestHeader('X-Requested-With','XMLHttpRequest');var l=this.headers;for(var n in l){if(!l.hasOwnProperty(n))continue;j.setRequestHeader(n,l[n])}var o=function(){j.abort()};Faye.Event.on(Faye.ENV,'beforeunload',o);var m=function(){Faye.Event.detach(Faye.ENV,'beforeunload',o);j.onreadystatechange=function(){};j=null};j.onreadystatechange=function(){if(j.readyState!==4)return;var a=null,b=j.status,d=((b>=200&&b<300)||b===304||b===1223);if(!d){m();h();return k.trigger('down')}try{a=JSON.parse(j.responseText)}catch(e){}m();if(a){k.receive(a);k.trigger('up')}else{h();k.trigger('down')}};j.send(Faye.toJSON(f))}}),{isUsable:function(a,b,d,f){d.call(f,Faye.URI.parse(b).isSameOrigin())}});Faye.Transport.register('long-polling',Faye.Transport.XHR);Faye.Transport.CORS=Faye.extend(Faye.Class(Faye.Transport,{request:function(b,d){var f=Faye.ENV.XDomainRequest?XDomainRequest:XMLHttpRequest,g=new f(),h=this.retry(b,d),i=this;g.open('POST',this.endpoint,true);if(g.setRequestHeader)g.setRequestHeader('Pragma','no-cache');var k=function(){if(!g)return false;g.onload=g.onerror=g.ontimeout=g.onprogress=null;g=null;Faye.ENV.clearTimeout(l);return true};g.onload=function(){var a=null;try{a=JSON.parse(g.responseText)}catch(e){}k();if(a){i.receive(a);i.trigger('up')}else{h();i.trigger('down')}};var j=function(){k();h();i.trigger('down')};var l=Faye.ENV.setTimeout(j,1.5*1000*d);g.onerror=j;g.ontimeout=j;g.onprogress=function(){};g.send('message='+encodeURIComponent(Faye.toJSON(b)))}}),{isUsable:function(a,b,d,f){if(Faye.URI.parse(b).isSameOrigin())return d.call(f,false);if(Faye.ENV.XDomainRequest)return d.call(f,Faye.URI.parse(b).protocol===Faye.URI.parse(Faye.ENV.location).protocol);if(Faye.ENV.XMLHttpRequest){var g=new Faye.ENV.XMLHttpRequest();return d.call(f,g.withCredentials!==undefined)}return d.call(f,false)}});Faye.Transport.register('cross-origin-long-polling',Faye.Transport.CORS);Faye.Transport.JSONP=Faye.extend(Faye.Class(Faye.Transport,{shouldFlush:function(a){var b={message:Faye.toJSON(a),jsonp:'__jsonp'+Faye.Transport.JSONP._v+'__'};var d=Faye.URI.parse(this.endpoint,b).toURL();return d.length>=Faye.Transport.MAX_URL_LENGTH},request:function(b,d){var f={message:Faye.toJSON(b)},g=document.getElementsByTagName('head')[0],h=document.createElement('script'),i=Faye.Transport.JSONP.getCallbackName(),k=Faye.URI.parse(this.endpoint,f),j=this.retry(b,d),l=this;Faye.ENV[i]=function(a){o();l.receive(a);l.trigger('up')};var n=Faye.ENV.setTimeout(function(){o();j();l.trigger('down')},1.5*1000*d);var o=function(){if(!Faye.ENV[i])return false;Faye.ENV[i]=undefined;try{delete Faye.ENV[i]}catch(e){}Faye.ENV.clearTimeout(n);h.parentNode.removeChild(h);return true};k.params.jsonp=i;h.type='text/javascript';h.src=k.toURL();g.appendChild(h)}}),{_v:0,getCallbackName:function(){this._v+=1;return'__jsonp'+this._v+'__'},isUsable:function(a,b,d,f){d.call(f,true)}});Faye.Transport.register('callback-polling',Faye.Transport.JSONP);
1
+ 'use strict';var Faye={VERSION:'0.8.9',BAYEUX_VERSION:'1.0',ID_LENGTH:160,JSONP_CALLBACK:'jsonpcallback',CONNECTION_TYPES:['long-polling','cross-origin-long-polling','callback-polling','websocket','eventsource','in-process'],MANDATORY_CONNECTION_TYPES:['long-polling','callback-polling','in-process'],ENV:(typeof global==='undefined')?window:global,extend:function(a,b,d){if(!b)return a;for(var f in b){if(!b.hasOwnProperty(f))continue;if(a.hasOwnProperty(f)&&d===false)continue;if(a[f]!==b[f])a[f]=b[f]}return a},random:function(a){a=a||this.ID_LENGTH;if(a>32){var b=Math.ceil(a/32),d='';while(b--)d+=this.random(32);var f=d.split(''),g='';while(f.length>0)g+=f.pop();return g}var h=Math.pow(2,a)-1,i=h.toString(36).length,d=Math.floor(Math.random()*h).toString(36);while(d.length<i)d='0'+d;return d},clientIdFromMessages:function(a){var b=[].concat(a)[0];return b&&b.clientId},copyObject:function(a){var b,d,f;if(a instanceof Array){b=[];d=a.length;while(d--)b[d]=Faye.copyObject(a[d]);return b}else if(typeof a==='object'){b=(a===null)?null:{};for(f in a)b[f]=Faye.copyObject(a[f]);return b}else{return a}},commonElement:function(a,b){for(var d=0,f=a.length;d<f;d++){if(this.indexOf(b,a[d])!==-1)return a[d]}return null},indexOf:function(a,b){if(a.indexOf)return a.indexOf(b);for(var d=0,f=a.length;d<f;d++){if(a[d]===b)return d}return-1},map:function(a,b,d){if(a.map)return a.map(b,d);var f=[];if(a instanceof Array){for(var g=0,h=a.length;g<h;g++){f.push(b.call(d||null,a[g],g))}}else{for(var i in a){if(!a.hasOwnProperty(i))continue;f.push(b.call(d||null,i,a[i]))}}return f},filter:function(a,b,d){var f=[];for(var g=0,h=a.length;g<h;g++){if(b.call(d||null,a[g],g))f.push(a[g])}return f},asyncEach:function(a,b,d,f){var g=a.length,h=-1,i=0,k=false;var j=function(){i-=1;h+=1;if(h===g)return d&&d.call(f);b(a[h],n)};var m=function(){if(k)return;k=true;while(i>0)j();k=false};var n=function(){i+=1;m()};n()},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)},logger:function(a){if(typeof console!=='undefined')console.log(a)},timestamp:function(){var b=new Date(),d=b.getFullYear(),f=b.getMonth()+1,g=b.getDate(),h=b.getHours(),i=b.getMinutes(),k=b.getSeconds();var j=function(a){return a<10?'0'+a:String(a)};return j(d)+'-'+j(f)+'-'+j(g)+' '+j(h)+':'+j(i)+':'+j(k)}};if(typeof window!=='undefined')window.Faye=Faye;Faye.Class=function(a,b){if(typeof a!=='function'){b=a;a=Object}var d=function(){if(!this.initialize)return this;return this.initialize.apply(this,arguments)||this};var f=function(){};f.prototype=a.prototype;d.prototype=new f();Faye.extend(d.prototype,b);return d};Faye.Namespace=Faye.Class({initialize:function(){this._e={}},exists:function(a){return this._e.hasOwnProperty(a)},generate:function(){var a=Faye.random();while(this._e.hasOwnProperty(a))a=Faye.random();return this._e[a]=a},release:function(a){delete this._e[a]}});Faye.Error=Faye.Class({initialize:function(a,b,d){this.code=a;this.params=Array.prototype.slice.call(b);this.message=d},toString:function(){return this.code+':'+this.params.join(',')+':'+this.message}});Faye.Error.parse=function(a){a=a||'';if(!Faye.Grammar.ERROR.test(a))return new this(null,[],a);var b=a.split(':'),d=parseInt(b[0]),f=b[1].split(','),a=b[2];return new this(d,f,a)};Faye.Error.versionMismatch=function(){return new this(300,arguments,"Version mismatch").toString()};Faye.Error.conntypeMismatch=function(){return new this(301,arguments,"Connection types not supported").toString()};Faye.Error.extMismatch=function(){return new this(302,arguments,"Extension mismatch").toString()};Faye.Error.badRequest=function(){return new this(400,arguments,"Bad request").toString()};Faye.Error.clientUnknown=function(){return new this(401,arguments,"Unknown client").toString()};Faye.Error.parameterMissing=function(){return new this(402,arguments,"Missing required parameter").toString()};Faye.Error.channelForbidden=function(){return new this(403,arguments,"Forbidden channel").toString()};Faye.Error.channelUnknown=function(){return new this(404,arguments,"Unknown channel").toString()};Faye.Error.channelInvalid=function(){return new this(405,arguments,"Invalid channel").toString()};Faye.Error.extUnknown=function(){return new this(406,arguments,"Unknown extension").toString()};Faye.Error.publishFailed=function(){return new this(407,arguments,"Failed to publish").toString()};Faye.Error.serverError=function(){return new this(500,arguments,"Internal server error").toString()};Faye.Deferrable={callback:function(a,b){if(!a)return;if(this._v==='succeeded')return a.apply(b,this._j);this._k=this._k||[];this._k.push([a,b])},timeout:function(a,b){var d=this;var f=Faye.ENV.setTimeout(function(){d.setDeferredStatus('failed',b)},a*1000);this._w=f},errback:function(a,b){if(!a)return;if(this._v==='failed')return a.apply(b,this._j);this._l=this._l||[];this._l.push([a,b])},setDeferredStatus:function(){if(this._w)Faye.ENV.clearTimeout(this._w);var a=Array.prototype.slice.call(arguments),b=a.shift(),d;this._v=b;this._j=a;if(b==='succeeded')d=this._k;else if(b==='failed')d=this._l;if(!d)return;var f;while(f=d.shift())f[0].apply(f[1],this._j)}};Faye.Publisher={countListeners:function(a){if(!this._4||!this._4[a])return 0;return this._4[a].length},bind:function(a,b,d){this._4=this._4||{};var f=this._4[a]=this._4[a]||[];f.push([b,d])},unbind:function(a,b,d){if(!this._4||!this._4[a])return;if(!b){delete this._4[a];return}var f=this._4[a],g=f.length;while(g--){if(b!==f[g][0])continue;if(d&&f[g][1]!==d)continue;f.splice(g,1)}},trigger:function(){var a=Array.prototype.slice.call(arguments),b=a.shift();if(!this._4||!this._4[b])return;var d=this._4[b].slice(),f;for(var g=0,h=d.length;g<h;g++){f=d[g];f[0].apply(f[1],a)}}};Faye.Timeouts={addTimeout:function(a,b,d,f){this._6=this._6||{};if(this._6.hasOwnProperty(a))return;var g=this;this._6[a]=Faye.ENV.setTimeout(function(){delete g._6[a];d.call(f)},1000*b)},removeTimeout:function(a){this._6=this._6||{};var b=this._6[a];if(!b)return;clearTimeout(b);delete this._6[a]}};Faye.Logging={LOG_LEVELS:{error:3,warn:2,info:1,debug:0},logLevel:'error',log:function(a,b){if(!Faye.logger)return;var d=Faye.Logging.LOG_LEVELS;if(d[Faye.Logging.logLevel]>d[b])return;var a=Array.prototype.slice.apply(a),f=' ['+b.toUpperCase()+'] [Faye',g=this.className,h=a.shift().replace(/\?/g,function(){try{return Faye.toJSON(a.shift())}catch(e){return'[Object]'}});for(var i in Faye){if(g)continue;if(typeof Faye[i]!=='function')continue;if(this instanceof Faye[i])g=i}if(g)f+='.'+g;f+='] ';Faye.logger(Faye.timestamp()+f+h)}};(function(){for(var d in Faye.Logging.LOG_LEVELS)(function(a,b){Faye.Logging[a]=function(){this.log(arguments,a)}})(d,Faye.Logging.LOG_LEVELS[d])})();Faye.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])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)$/};Faye.Extensible={addExtension:function(a){this._7=this._7||[];this._7.push(a);if(a.added)a.added(this)},removeExtension:function(a){if(!this._7)return;var b=this._7.length;while(b--){if(this._7[b]!==a)continue;this._7.splice(b,1);if(a.removed)a.removed(this)}},pipeThroughExtensions:function(d,f,g,h){this.debug('Passing through ? extensions: ?',d,f);if(!this._7)return g.call(h,f);var i=this._7.slice();var k=function(a){if(!a)return g.call(h,a);var b=i.shift();if(!b)return g.call(h,a);if(b[d])b[d](a,k);else k(a)};k(f)}};Faye.extend(Faye.Extensible,Faye.Logging);Faye.Channel=Faye.Class({initialize:function(a){this.id=this.name=a},push:function(a){this.trigger('message',a)},isUnused:function(){return this.countListeners('message')===0}});Faye.extend(Faye.Channel.prototype,Faye.Publisher);Faye.extend(Faye.Channel,{HANDSHAKE:'/meta/handshake',CONNECT:'/meta/connect',SUBSCRIBE:'/meta/subscribe',UNSUBSCRIBE:'/meta/unsubscribe',DISCONNECT:'/meta/disconnect',META:'meta',SERVICE:'service',expand:function(a){var b=this.parse(a),d=['/**',a];var f=b.slice();f[f.length-1]='*';d.push(this.unparse(f));for(var g=1,h=b.length;g<h;g++){f=b.slice(0,g);f.push('**');d.push(this.unparse(f))}return d},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)},unparse:function(a){return'/'+a.join('/')},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)},Set:Faye.Class({initialize:function(){this._2={}},getKeys:function(){var a=[];for(var b in this._2)a.push(b);return a},remove:function(a){delete this._2[a]},hasSubscription:function(a){return this._2.hasOwnProperty(a)},subscribe:function(a,b,d){if(!b)return;var f;for(var g=0,h=a.length;g<h;g++){f=a[g];var i=this._2[f]=this._2[f]||new Faye.Channel(f);i.bind('message',b,d)}},unsubscribe:function(a,b,d){var f=this._2[a];if(!f)return false;f.unbind('message',b,d);if(f.isUnused()){this.remove(a);return true}else{return false}},distributeMessage:function(a){var b=Faye.Channel.expand(a.channel);for(var d=0,f=b.length;d<f;d++){var g=this._2[b[d]];if(g)g.trigger('message',a.data)}}})});Faye.Publication=Faye.Class(Faye.Deferrable);Faye.Subscription=Faye.Class({initialize:function(a,b,d,f){this._8=a;this._2=b;this._m=d;this._n=f;this._x=false},cancel:function(){if(this._x)return;this._8.unsubscribe(this._2,this._m,this._n);this._x=true},unsubscribe:function(){this.cancel()}});Faye.extend(Faye.Subscription.prototype,Faye.Deferrable);Faye.Client=Faye.Class({UNCONNECTED:1,CONNECTING:2,CONNECTED:3,DISCONNECTED:4,HANDSHAKE:'handshake',RETRY:'retry',NONE:'none',CONNECTION_TIMEOUT:60.0,DEFAULT_RETRY:5.0,DEFAULT_ENDPOINT:'/bayeux',INTERVAL:0.0,initialize:function(a,b){this.info('New client created for ?',a);this._f=b||{};this.endpoint=a||this.DEFAULT_ENDPOINT;this.endpoints=this._f.endpoints||{};this.transports={};this._D=Faye.CookieJar&&new Faye.CookieJar();this._y={};this._o=[];this.retry=this._f.retry||this.DEFAULT_RETRY;this._1=this.UNCONNECTED;this._2=new Faye.Channel.Set();this._g=0;this._p={};this._9={reconnect:this.RETRY,interval:1000*(this._f.interval||this.INTERVAL),timeout:1000*(this._f.timeout||this.CONNECTION_TIMEOUT)};if(Faye.Event)Faye.Event.on(Faye.ENV,'beforeunload',function(){if(Faye.indexOf(this._o,'autodisconnect')<0)this.disconnect()},this)},disable:function(a){this._o.push(a)},setHeader:function(a,b){this._y[a]=b},getClientId:function(){return this._0},getState:function(){switch(this._1){case this.UNCONNECTED:return'UNCONNECTED';case this.CONNECTING:return'CONNECTING';case this.CONNECTED:return'CONNECTED';case this.DISCONNECTED:return'DISCONNECTED'}},handshake:function(b,d){if(this._9.reconnect===this.NONE)return;if(this._1!==this.UNCONNECTED)return;this._1=this.CONNECTING;var f=this;this.info('Initiating handshake with ?',this.endpoint);this._z(Faye.MANDATORY_CONNECTION_TYPES);this._a({channel:Faye.Channel.HANDSHAKE,version:Faye.BAYEUX_VERSION,supportedConnectionTypes:[this._5.connectionType]},function(a){if(a.successful){this._1=this.CONNECTED;this._0=a.clientId;this._z(a.supportedConnectionTypes);this.info('Handshake successful: ?',this._0);this.subscribe(this._2.getKeys(),true);if(b)b.call(d)}else{this.info('Handshake unsuccessful');Faye.ENV.setTimeout(function(){f.handshake(b,d)},this._9.interval);this._1=this.UNCONNECTED}},this)},connect:function(a,b){if(this._9.reconnect===this.NONE)return;if(this._1===this.DISCONNECTED)return;if(this._1===this.UNCONNECTED)return this.handshake(function(){this.connect(a,b)},this);this.callback(a,b);if(this._1!==this.CONNECTED)return;this.info('Calling deferred actions for ?',this._0);this.setDeferredStatus('succeeded');this.setDeferredStatus('deferred');if(this._q)return;this._q=true;this.info('Initiating connection for ?',this._0);this._a({channel:Faye.Channel.CONNECT,clientId:this._0,connectionType:this._5.connectionType},this._A,this)},disconnect:function(){if(this._1!==this.CONNECTED)return;this._1=this.DISCONNECTED;this.info('Disconnecting ?',this._0);this._a({channel:Faye.Channel.DISCONNECT,clientId:this._0},function(a){if(a.successful)this._5.close()},this);this.info('Clearing channel listeners for ?',this._0);this._2=new Faye.Channel.Set()},subscribe:function(d,f,g){if(d instanceof Array)return Faye.map(d,function(c){return this.subscribe(c,f,g)},this);var h=new Faye.Subscription(this,d,f,g),i=(f===true),k=this._2.hasSubscription(d);if(k&&!i){this._2.subscribe([d],f,g);h.setDeferredStatus('succeeded');return h}this.connect(function(){this.info('Client ? attempting to subscribe to ?',this._0,d);if(!i)this._2.subscribe([d],f,g);this._a({channel:Faye.Channel.SUBSCRIBE,clientId:this._0,subscription:d},function(a){if(!a.successful){h.setDeferredStatus('failed',Faye.Error.parse(a.error));return this._2.unsubscribe(d,f,g)}var b=[].concat(a.subscription);this.info('Subscription acknowledged for ? to ?',this._0,b);h.setDeferredStatus('succeeded')},this)},this);return h},unsubscribe:function(d,f,g){if(d instanceof Array)return Faye.map(d,function(c){return this.unsubscribe(c,f,g)},this);var h=this._2.unsubscribe(d,f,g);if(!h)return;this.connect(function(){this.info('Client ? attempting to unsubscribe from ?',this._0,d);this._a({channel:Faye.Channel.UNSUBSCRIBE,clientId:this._0,subscription:d},function(a){if(!a.successful)return;var b=[].concat(a.subscription);this.info('Unsubscription acknowledged for ? from ?',this._0,b)},this)},this)},publish:function(b,d){var f=new Faye.Publication();this.connect(function(){this.info('Client ? queueing published message to ?: ?',this._0,b,d);this._a({channel:b,data:d,clientId:this._0},function(a){if(a.successful)f.setDeferredStatus('succeeded');else f.setDeferredStatus('failed',Faye.Error.parse(a.error))},this)},this);return f},receiveMessage:function(d){this.pipeThroughExtensions('incoming',d,function(a){if(!a)return;if(a.advice)this._E(a.advice);this._F(a);if(a.successful===undefined)return;var b=this._p[a.id];if(!b)return;delete this._p[a.id];b[0].call(b[1],a)},this)},_z:function(b){Faye.Transport.get(this,b,this._o,function(a){this.debug('Selected ? transport for ?',a.connectionType,a.endpoint);if(a===this._5)return;if(this._5)this._5.close();this._5=a;this._5.cookies=this._D;this._5.headers=this._y;a.bind('down',function(){if(this._b!==undefined&&!this._b)return;this._b=false;this.trigger('transport:down')},this);a.bind('up',function(){if(this._b!==undefined&&this._b)return;this._b=true;this.trigger('transport:up')},this)},this)},_a:function(b,d,f){b.id=this._G();if(d)this._p[b.id]=[d,f];this.pipeThroughExtensions('outgoing',b,function(a){if(!a)return;this._5.send(a,this._9.timeout/1000)},this)},_G:function(){this._g+=1;if(this._g>=Math.pow(2,32))this._g=0;return this._g.toString(36)},_E:function(a){Faye.extend(this._9,a);if(this._9.reconnect===this.HANDSHAKE&&this._1!==this.DISCONNECTED){this._1=this.UNCONNECTED;this._0=null;this._A()}},_F:function(a){if(!a.channel||a.data===undefined)return;this.info('Client ? calling listeners for ? with ?',this._0,a.channel,a.data);this._2.distributeMessage(a)},_H:function(){if(!this._q)return;this._q=null;this.info('Closed connection for ?',this._0)},_A:function(){this._H();var a=this;Faye.ENV.setTimeout(function(){a.connect()},this._9.interval)}});Faye.extend(Faye.Client.prototype,Faye.Deferrable);Faye.extend(Faye.Client.prototype,Faye.Publisher);Faye.extend(Faye.Client.prototype,Faye.Logging);Faye.extend(Faye.Client.prototype,Faye.Extensible);Faye.Transport=Faye.extend(Faye.Class({MAX_DELAY:0.0,batching:true,initialize:function(a,b){this._8=a;this.endpoint=b;this._c=[]},close:function(){},send:function(a,b){this.debug('Client ? sending message to ?: ?',this._8._0,this.endpoint,a);if(!this.batching)return this.request([a],b);this._c.push(a);this._I=b;if(a.channel===Faye.Channel.HANDSHAKE)return this.addTimeout('publish',0.01,this.flush,this);if(a.channel===Faye.Channel.CONNECT)this._r=a;if(this.shouldFlush&&this.shouldFlush(this._c))return this.flush();this.addTimeout('publish',this.MAX_DELAY,this.flush,this)},flush:function(){this.removeTimeout('publish');if(this._c.length>1&&this._r)this._r.advice={timeout:0};this.request(this._c,this._I);this._r=null;this._c=[]},receive:function(a){this.debug('Client ? received from ?: ?',this._8._0,this.endpoint,a);for(var b=0,d=a.length;b<d;b++){this._8.receiveMessage(a[b])}},retry:function(a,b){var d=false,f=this._8.retry*1000,g=this;return function(){if(d)return;d=true;Faye.ENV.setTimeout(function(){g.request(a,b)},f)}}}),{MAX_URL_LENGTH:2048,get:function(k,j,m,n,o){var l=k.endpoint;Faye.asyncEach(this._B,function(d,f){var g=d[0],h=d[1],i=k.endpoints[g]||l;if(Faye.indexOf(m,g)>=0)return f();if(Faye.indexOf(j,g)<0){h.isUsable(k,i,function(){});return f()}h.isUsable(k,i,function(a){if(!a)return f();var b=h.hasOwnProperty('create')?h.create(k,i):new h(k,i);n.call(o,b)})},function(){throw new Error('Could not find a usable connection type for '+l);})},register:function(a,b){this._B.push([a,b]);b.prototype.connectionType=a},_B:[]});Faye.extend(Faye.Transport.prototype,Faye.Logging);Faye.extend(Faye.Transport.prototype,Faye.Publisher);Faye.extend(Faye.Transport.prototype,Faye.Timeouts);Faye.Event={_h:[],on:function(a,b,d,f){var g=function(){d.call(f)};if(a.addEventListener)a.addEventListener(b,g,false);else a.attachEvent('on'+b,g);this._h.push({_i:a,_s:b,_m:d,_n:f,_C:g})},detach:function(a,b,d,f){var g=this._h.length,h;while(g--){h=this._h[g];if((a&&a!==h._i)||(b&&b!==h._s)||(d&&d!==h._m)||(f&&f!==h._n))continue;if(h._i.removeEventListener)h._i.removeEventListener(h._s,h._C,false);else h._i.detachEvent('on'+h._s,h._C);this._h.splice(g,1);h=null}}};Faye.Event.on(Faye.ENV,'unload',Faye.Event.detach,Faye.Event);Faye.URI=Faye.extend(Faye.Class({queryString:function(){var a=[];for(var b in this.params){if(!this.params.hasOwnProperty(b))continue;a.push(encodeURIComponent(b)+'='+encodeURIComponent(this.params[b]))}return a.join('&')},isSameOrigin:function(){var a=Faye.URI.parse(Faye.ENV.location.href,false);var b=(a.hostname!==this.hostname)||(a.port!==this.port)||(a.protocol!==this.protocol);return!b},toURL:function(){var a=this.queryString();return this.protocol+'//'+this.hostname+(this.port?':'+this.port:'')+this.pathname+(a?'?'+a:'')+this.hash}}),{parse:function(g,h){if(typeof g!=='string')return g;var i=new this(),k;var j=function(b,d,f){g=g.replace(d,function(a){i[b]=a;return''});if(i[b]===undefined)i[b]=f?Faye.ENV.location[b]:''};j('protocol',/^https?\:/,true);j('host',/^\/\/[^\/]+/,true);if(!/^\//.test(g))g=Faye.ENV.location.pathname.replace(/[^\/]*$/,'')+g;j('pathname',/^\/[^\?#]*/);j('search',/^\?[^#]*/);j('hash',/^#.*/);if(/^\/\//.test(i.host)){i.host=i.host.substr(2);k=i.host.split(':');i.hostname=k[0];i.port=k[1]||''}else{i.hostname=Faye.ENV.location.hostname;i.port=Faye.ENV.location.port}if(h===false){i.params={}}else{var m=i.search.replace(/^\?/,''),n=m?m.split('&'):[],o=n.length,l={};while(o--){k=n[o].split('=');l[decodeURIComponent(k[0]||'')]=decodeURIComponent(k[1]||'')}if(typeof h==='object')Faye.extend(l,h);i.params=l}return i}});if(!this.JSON){JSON={}}(function(){function m(a){return a<10?'0'+a:a}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(a){return this.getUTCFullYear()+'-'+m(this.getUTCMonth()+1)+'-'+m(this.getUTCDate())+'T'+m(this.getUTCHours())+':'+m(this.getUTCMinutes())+':'+m(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,l,q,t={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},p;function s(d){o.lastIndex=0;return o.test(d)?'"'+d.replace(o,function(a){var b=t[a];return typeof b==='string'?b:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+d+'"'}function r(a,b){var d,f,g,h,i=l,k,j=b[a];if(j&&typeof j==='object'&&typeof j.toJSON==='function'){j=j.toJSON(a)}if(typeof p==='function'){j=p.call(b,a,j)}switch(typeof j){case'string':return s(j);case'number':return isFinite(j)?String(j):'null';case'boolean':case'null':return String(j);case'object':if(!j){return'null'}l+=q;k=[];if(Object.prototype.toString.apply(j)==='[object Array]'){h=j.length;for(d=0;d<h;d+=1){k[d]=r(d,j)||'null'}g=k.length===0?'[]':l?'[\n'+l+k.join(',\n'+l)+'\n'+i+']':'['+k.join(',')+']';l=i;return g}if(p&&typeof p==='object'){h=p.length;for(d=0;d<h;d+=1){f=p[d];if(typeof f==='string'){g=r(f,j);if(g){k.push(s(f)+(l?': ':':')+g)}}}}else{for(f in j){if(Object.hasOwnProperty.call(j,f)){g=r(f,j);if(g){k.push(s(f)+(l?': ':':')+g)}}}}g=k.length===0?'{}':l?'{\n'+l+k.join(',\n'+l)+'\n'+i+'}':'{'+k.join(',')+'}';l=i;return g}}Faye.stringify=function(a,b,d){var f;l='';q='';if(typeof d==='number'){for(f=0;f<d;f+=1){q+=' '}}else if(typeof d==='string'){q=d}p=b;if(b&&typeof b!=='function'&&(typeof b!=='object'||typeof b.length!=='number')){throw new Error('JSON.stringify');}return r('',{'':a})};if(typeof JSON.stringify!=='function'){JSON.stringify=Faye.stringify}if(typeof JSON.parse!=='function'){JSON.parse=function(h,i){var k;function j(a,b){var d,f,g=a[b];if(g&&typeof g==='object'){for(d in g){if(Object.hasOwnProperty.call(g,d)){f=j(g,d);if(f!==undefined){g[d]=f}else{delete g[d]}}}}return i.call(a,b,g)}n.lastIndex=0;if(n.test(h)){h=h.replace(n,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(h.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){k=eval('('+h+')');return typeof i==='function'?j({'':k},''):k}throw new SyntaxError('JSON.parse');}}}());Faye.Transport.WebSocket=Faye.extend(Faye.Class(Faye.Transport,{UNCONNECTED:1,CONNECTING:2,CONNECTED:3,batching:false,isUsable:function(a,b){this.callback(function(){a.call(b,true)});this.errback(function(){a.call(b,false)});this.connect()},request:function(b,d){if(b.length===0)return;this._d=this._d||{};for(var f=0,g=b.length;f<g;f++){this._d[b[f].id]=b[f]}this.callback(function(a){a.send(Faye.toJSON(b))});this.connect()},close:function(){if(!this._3)return;this._3.onclose=this._3.onerror=null;this._3.close();delete this._3;this.setDeferredStatus('deferred');this._1=this.UNCONNECTED},connect:function(){if(Faye.Transport.WebSocket._J)return;this._1=this._1||this.UNCONNECTED;if(this._1!==this.UNCONNECTED)return;this._1=this.CONNECTING;var g=Faye.Transport.WebSocket.getClass();if(!g)return this.setDeferredStatus('failed');this._3=new g(Faye.Transport.WebSocket.getSocketUrl(this.endpoint));var h=this;this._3.onopen=function(){h._1=h.CONNECTED;h._t=true;h.setDeferredStatus('succeeded',h._3);h.trigger('up')};this._3.onmessage=function(a){var b=JSON.parse(a.data);if(!b)return;b=[].concat(b);for(var d=0,f=b.length;d<f;d++){delete h._d[b[d].id]}h.receive(b)};this._3.onclose=this._3.onerror=function(){var a=(h._1===h.CONNECTED);h.setDeferredStatus('deferred');h._1=h.UNCONNECTED;h.close();if(a)return h.resend();if(!h._t)return h.setDeferredStatus('failed');var b=h._8.retry*1000;Faye.ENV.setTimeout(function(){h.connect()},b);h.trigger('down')}},resend:function(){if(!this._d)return;var d=Faye.map(this._d,function(a,b){return b});this.request(d)}}),{getSocketUrl:function(a){if(Faye.URI)a=Faye.URI.parse(a).toURL();return a.replace(/^http(s?):/ig,'ws$1:')},getClass:function(){return(Faye.WebSocket&&Faye.WebSocket.Client)||Faye.ENV.WebSocket||Faye.ENV.MozWebSocket},isUsable:function(a,b,d,f){this.create(a,b).isUsable(d,f)},create:function(a,b){var d=a.transports.websocket=a.transports.websocket||{};d[b]=d[b]||new this(a,b);return d[b]}});Faye.extend(Faye.Transport.WebSocket.prototype,Faye.Deferrable);Faye.Transport.register('websocket',Faye.Transport.WebSocket);if(Faye.Event)Faye.Event.on(Faye.ENV,'beforeunload',function(){Faye.Transport.WebSocket._J=true});Faye.Transport.EventSource=Faye.extend(Faye.Class(Faye.Transport,{initialize:function(b,d){Faye.Transport.prototype.initialize.call(this,b,d);if(!Faye.ENV.EventSource)return this.setDeferredStatus('failed');this._K=new Faye.Transport.XHR(b,d);var f=new EventSource(d+'/'+b.getClientId()),g=this;f.onopen=function(){g._t=true;g.setDeferredStatus('succeeded');g.trigger('up')};f.onerror=function(){if(g._t){g.trigger('down')}else{g.setDeferredStatus('failed');f.close()}};f.onmessage=function(a){g.receive(JSON.parse(a.data));g.trigger('up')};this._3=f},isUsable:function(a,b){this.callback(function(){a.call(b,true)});this.errback(function(){a.call(b,false)})},request:function(a,b){this._K.request(a,b)},close:function(){if(!this._3)return;this._3.onerror=null;this._3.close();delete this._3}}),{isUsable:function(b,d,f,g){var h=b.getClientId();if(!h)return f.call(g,false);Faye.Transport.XHR.isUsable(b,d,function(a){if(!a)return f.call(g,false);this.create(b,d).isUsable(f,g)},this)},create:function(a,b){var d=a.transports.eventsource=a.transports.eventsource||{},f=a.getClientId(),b=b+'/'+(f||'');d[b]=d[b]||new this(a,b);return d[b]}});Faye.extend(Faye.Transport.EventSource.prototype,Faye.Deferrable);Faye.Transport.register('eventsource',Faye.Transport.EventSource);Faye.Transport.XHR=Faye.extend(Faye.Class(Faye.Transport,{request:function(f,g){var h=this.retry(f,g),i=Faye.URI.parse(this.endpoint).pathname,k=this,j=Faye.ENV.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();j.open('POST',i,true);j.setRequestHeader('Content-Type','application/json');j.setRequestHeader('Pragma','no-cache');j.setRequestHeader('X-Requested-With','XMLHttpRequest');var m=this.headers;for(var n in m){if(!m.hasOwnProperty(n))continue;j.setRequestHeader(n,m[n])}var o=function(){j.abort()};Faye.Event.on(Faye.ENV,'beforeunload',o);var l=function(){Faye.Event.detach(Faye.ENV,'beforeunload',o);j.onreadystatechange=function(){};j=null};j.onreadystatechange=function(){if(j.readyState!==4)return;var a=null,b=j.status,d=((b>=200&&b<300)||b===304||b===1223);if(!d){l();h();return k.trigger('down')}try{a=JSON.parse(j.responseText)}catch(e){}l();if(a){k.receive(a);k.trigger('up')}else{h();k.trigger('down')}};j.send(Faye.toJSON(f))}}),{isUsable:function(a,b,d,f){d.call(f,Faye.URI.parse(b).isSameOrigin())}});Faye.Transport.register('long-polling',Faye.Transport.XHR);Faye.Transport.CORS=Faye.extend(Faye.Class(Faye.Transport,{request:function(b,d){var f=Faye.ENV.XDomainRequest?XDomainRequest:XMLHttpRequest,g=new f(),h=this.retry(b,d),i=this;g.open('POST',this.endpoint,true);if(g.setRequestHeader)g.setRequestHeader('Pragma','no-cache');var k=function(){if(!g)return false;g.onload=g.onerror=g.ontimeout=g.onprogress=null;g=null;Faye.ENV.clearTimeout(m);return true};g.onload=function(){var a=null;try{a=JSON.parse(g.responseText)}catch(e){}k();if(a){i.receive(a);i.trigger('up')}else{h();i.trigger('down')}};var j=function(){k();h();i.trigger('down')};var m=Faye.ENV.setTimeout(j,1.5*1000*d);g.onerror=j;g.ontimeout=j;g.onprogress=function(){};g.send('message='+encodeURIComponent(Faye.toJSON(b)))}}),{isUsable:function(a,b,d,f){if(Faye.URI.parse(b).isSameOrigin())return d.call(f,false);if(Faye.ENV.XDomainRequest)return d.call(f,Faye.URI.parse(b).protocol===Faye.URI.parse(Faye.ENV.location).protocol);if(Faye.ENV.XMLHttpRequest){var g=new Faye.ENV.XMLHttpRequest();return d.call(f,g.withCredentials!==undefined)}return d.call(f,false)}});Faye.Transport.register('cross-origin-long-polling',Faye.Transport.CORS);Faye.Transport.JSONP=Faye.extend(Faye.Class(Faye.Transport,{shouldFlush:function(a){var b={message:Faye.toJSON(a),jsonp:'__jsonp'+Faye.Transport.JSONP._u+'__'};var d=Faye.URI.parse(this.endpoint,b).toURL();return d.length>=Faye.Transport.MAX_URL_LENGTH},request:function(b,d){var f={message:Faye.toJSON(b)},g=document.getElementsByTagName('head')[0],h=document.createElement('script'),i=Faye.Transport.JSONP.getCallbackName(),k=Faye.URI.parse(this.endpoint,f),j=this.retry(b,d),m=this;Faye.ENV[i]=function(a){o();m.receive(a);m.trigger('up')};var n=Faye.ENV.setTimeout(function(){o();j();m.trigger('down')},1.5*1000*d);var o=function(){if(!Faye.ENV[i])return false;Faye.ENV[i]=undefined;try{delete Faye.ENV[i]}catch(e){}Faye.ENV.clearTimeout(n);h.parentNode.removeChild(h);return true};k.params.jsonp=i;h.type='text/javascript';h.src=k.toURL();g.appendChild(h)}}),{_u:0,getCallbackName:function(){this._u+=1;return'__jsonp'+this._u+'__'},isUsable:function(a,b,d,f){d.call(f,true)}});Faye.Transport.register('callback-polling',Faye.Transport.JSONP);
2
2
  //@ sourceMappingURL=faye-browser-min.js.map
@@ -1,29 +1,28 @@
1
- var Faye = (typeof Faye === 'object') ? Faye : {};
2
- if (typeof window !== 'undefined') window.Faye = Faye;
3
-
4
- Faye.extend = function(dest, source, overwrite) {
5
- if (!source) return dest;
6
- for (var key in source) {
7
- if (!source.hasOwnProperty(key)) continue;
8
- if (dest.hasOwnProperty(key) && overwrite === false) continue;
9
- if (dest[key] !== source[key])
10
- dest[key] = source[key];
11
- }
12
- return dest;
13
- };
1
+ 'use strict';
2
+
3
+ var Faye = {
4
+ VERSION: '0.8.9',
14
5
 
15
- Faye.extend(Faye, {
16
- VERSION: '0.8.8',
17
-
18
6
  BAYEUX_VERSION: '1.0',
19
7
  ID_LENGTH: 160,
20
8
  JSONP_CALLBACK: 'jsonpcallback',
21
9
  CONNECTION_TYPES: ['long-polling', 'cross-origin-long-polling', 'callback-polling', 'websocket', 'eventsource', 'in-process'],
22
-
10
+
23
11
  MANDATORY_CONNECTION_TYPES: ['long-polling', 'callback-polling', 'in-process'],
24
-
25
- ENV: (function() { return this })(),
26
-
12
+
13
+ ENV: (typeof global === 'undefined') ? window : global,
14
+
15
+ extend: function(dest, source, overwrite) {
16
+ if (!source) return dest;
17
+ for (var key in source) {
18
+ if (!source.hasOwnProperty(key)) continue;
19
+ if (dest.hasOwnProperty(key) && overwrite === false) continue;
20
+ if (dest[key] !== source[key])
21
+ dest[key] = source[key];
22
+ }
23
+ return dest;
24
+ },
25
+
27
26
  random: function(bitlength) {
28
27
  bitlength = bitlength || this.ID_LENGTH;
29
28
  if (bitlength > 32) {
@@ -37,16 +36,16 @@ Faye.extend(Faye, {
37
36
  var limit = Math.pow(2, bitlength) - 1,
38
37
  maxSize = limit.toString(36).length,
39
38
  string = Math.floor(Math.random() * limit).toString(36);
40
-
39
+
41
40
  while (string.length < maxSize) string = '0' + string;
42
41
  return string;
43
42
  },
44
-
43
+
45
44
  clientIdFromMessages: function(messages) {
46
45
  var first = [].concat(messages)[0];
47
46
  return first && first.clientId;
48
47
  },
49
-
48
+
50
49
  copyObject: function(object) {
51
50
  var clone, i, key;
52
51
  if (object instanceof Array) {
@@ -62,7 +61,7 @@ Faye.extend(Faye, {
62
61
  return object;
63
62
  }
64
63
  },
65
-
64
+
66
65
  commonElement: function(lista, listb) {
67
66
  for (var i = 0, n = lista.length; i < n; i++) {
68
67
  if (this.indexOf(listb, lista[i]) !== -1)
@@ -70,20 +69,20 @@ Faye.extend(Faye, {
70
69
  }
71
70
  return null;
72
71
  },
73
-
72
+
74
73
  indexOf: function(list, needle) {
75
74
  if (list.indexOf) return list.indexOf(needle);
76
-
75
+
77
76
  for (var i = 0, n = list.length; i < n; i++) {
78
77
  if (list[i] === needle) return i;
79
78
  }
80
79
  return -1;
81
80
  },
82
-
81
+
83
82
  map: function(object, callback, context) {
84
83
  if (object.map) return object.map(callback, context);
85
84
  var result = [];
86
-
85
+
87
86
  if (object instanceof Array) {
88
87
  for (var i = 0, n = object.length; i < n; i++) {
89
88
  result.push(callback.call(context || null, object[i], i));
@@ -96,7 +95,7 @@ Faye.extend(Faye, {
96
95
  }
97
96
  return result;
98
97
  },
99
-
98
+
100
99
  filter: function(array, callback, context) {
101
100
  var result = [];
102
101
  for (var i = 0, n = array.length; i < n; i++) {
@@ -105,7 +104,7 @@ Faye.extend(Faye, {
105
104
  }
106
105
  return result;
107
106
  },
108
-
107
+
109
108
  asyncEach: function(list, iterator, callback, context) {
110
109
  var n = list.length,
111
110
  i = -1,
@@ -132,7 +131,7 @@ Faye.extend(Faye, {
132
131
  };
133
132
  resume();
134
133
  },
135
-
134
+
136
135
  // http://assanka.net/content/tech/2009/09/02/json2-js-vs-prototype/
137
136
  toJSON: function(object) {
138
137
  if (this.stringify)
@@ -141,14 +140,14 @@ Faye.extend(Faye, {
141
140
  ? this[key]
142
141
  : value;
143
142
  });
144
-
143
+
145
144
  return JSON.stringify(object);
146
145
  },
147
-
146
+
148
147
  logger: function(message) {
149
148
  if (typeof console !== 'undefined') console.log(message);
150
149
  },
151
-
150
+
152
151
  timestamp: function() {
153
152
  var date = new Date(),
154
153
  year = date.getFullYear(),
@@ -157,15 +156,18 @@ Faye.extend(Faye, {
157
156
  hour = date.getHours(),
158
157
  minute = date.getMinutes(),
159
158
  second = date.getSeconds();
160
-
159
+
161
160
  var pad = function(n) {
162
161
  return n < 10 ? '0' + n : String(n);
163
162
  };
164
-
163
+
165
164
  return pad(year) + '-' + pad(month) + '-' + pad(day) + ' ' +
166
165
  pad(hour) + ':' + pad(minute) + ':' + pad(second);
167
166
  }
168
- });
167
+ };
168
+
169
+ if (typeof window !== 'undefined')
170
+ window.Faye = Faye;
169
171
 
170
172
 
171
173
  Faye.Class = function(parent, methods) {
@@ -173,18 +175,18 @@ Faye.Class = function(parent, methods) {
173
175
  methods = parent;
174
176
  parent = Object;
175
177
  }
176
-
178
+
177
179
  var klass = function() {
178
180
  if (!this.initialize) return this;
179
181
  return this.initialize.apply(this, arguments) || this;
180
182
  };
181
-
183
+
182
184
  var bridge = function() {};
183
185
  bridge.prototype = parent.prototype;
184
-
186
+
185
187
  klass.prototype = new bridge();
186
188
  Faye.extend(klass.prototype, methods);
187
-
189
+
188
190
  return klass;
189
191
  };
190
192
 
@@ -193,18 +195,18 @@ Faye.Namespace = Faye.Class({
193
195
  initialize: function() {
194
196
  this._used = {};
195
197
  },
196
-
198
+
197
199
  exists: function(id) {
198
200
  return this._used.hasOwnProperty(id);
199
201
  },
200
-
202
+
201
203
  generate: function() {
202
204
  var name = Faye.random();
203
205
  while (this._used.hasOwnProperty(name))
204
206
  name = Faye.random();
205
207
  return this._used[name] = name;
206
208
  },
207
-
209
+
208
210
  release: function(id) {
209
211
  delete this._used[id];
210
212
  }
@@ -217,7 +219,7 @@ Faye.Error = Faye.Class({
217
219
  this.params = Array.prototype.slice.call(params);
218
220
  this.message = message;
219
221
  },
220
-
222
+
221
223
  toString: function() {
222
224
  return this.code + ':' +
223
225
  this.params.join(',') + ':' +
@@ -278,14 +280,14 @@ Faye.Error.serverError = function() {
278
280
  Faye.Deferrable = {
279
281
  callback: function(callback, context) {
280
282
  if (!callback) return;
281
-
283
+
282
284
  if (this._deferredStatus === 'succeeded')
283
285
  return callback.apply(context, this._deferredArgs);
284
-
286
+
285
287
  this._callbacks = this._callbacks || [];
286
288
  this._callbacks.push([callback, context]);
287
289
  },
288
-
290
+
289
291
  timeout: function(seconds, message) {
290
292
  var _this = this;
291
293
  var timer = Faye.ENV.setTimeout(function() {
@@ -293,7 +295,7 @@ Faye.Deferrable = {
293
295
  }, seconds * 1000);
294
296
  this._timer = timer;
295
297
  },
296
-
298
+
297
299
  errback: function(callback, context) {
298
300
  if (!callback) return;
299
301
 
@@ -311,17 +313,17 @@ Faye.Deferrable = {
311
313
  var args = Array.prototype.slice.call(arguments),
312
314
  status = args.shift(),
313
315
  callbacks;
314
-
316
+
315
317
  this._deferredStatus = status;
316
318
  this._deferredArgs = args;
317
-
319
+
318
320
  if (status === 'succeeded')
319
321
  callbacks = this._callbacks;
320
322
  else if (status === 'failed')
321
323
  callbacks = this._errbacks;
322
-
324
+
323
325
  if (!callbacks) return;
324
-
326
+
325
327
  var callback;
326
328
  while (callback = callbacks.shift())
327
329
  callback[0].apply(callback[1], this._deferredArgs);
@@ -334,39 +336,39 @@ Faye.Publisher = {
334
336
  if (!this._subscribers || !this._subscribers[eventType]) return 0;
335
337
  return this._subscribers[eventType].length;
336
338
  },
337
-
339
+
338
340
  bind: function(eventType, listener, context) {
339
341
  this._subscribers = this._subscribers || {};
340
342
  var list = this._subscribers[eventType] = this._subscribers[eventType] || [];
341
343
  list.push([listener, context]);
342
344
  },
343
-
345
+
344
346
  unbind: function(eventType, listener, context) {
345
347
  if (!this._subscribers || !this._subscribers[eventType]) return;
346
-
348
+
347
349
  if (!listener) {
348
350
  delete this._subscribers[eventType];
349
351
  return;
350
352
  }
351
353
  var list = this._subscribers[eventType],
352
354
  i = list.length;
353
-
355
+
354
356
  while (i--) {
355
357
  if (listener !== list[i][0]) continue;
356
358
  if (context && list[i][1] !== context) continue;
357
359
  list.splice(i,1);
358
360
  }
359
361
  },
360
-
362
+
361
363
  trigger: function() {
362
364
  var args = Array.prototype.slice.call(arguments),
363
365
  eventType = args.shift();
364
-
366
+
365
367
  if (!this._subscribers || !this._subscribers[eventType]) return;
366
-
368
+
367
369
  var listeners = this._subscribers[eventType].slice(),
368
370
  listener;
369
-
371
+
370
372
  for (var i = 0, n = listeners.length; i < n; i++) {
371
373
  listener = listeners[i];
372
374
  listener[0].apply(listener[1], args);
@@ -385,7 +387,7 @@ Faye.Timeouts = {
385
387
  callback.call(context);
386
388
  }, 1000 * delay);
387
389
  },
388
-
390
+
389
391
  removeTimeout: function(name) {
390
392
  this._timeouts = this._timeouts || {};
391
393
  var timeout = this._timeouts[name];
@@ -403,19 +405,19 @@ Faye.Logging = {
403
405
  info: 1,
404
406
  debug: 0
405
407
  },
406
-
408
+
407
409
  logLevel: 'error',
408
-
410
+
409
411
  log: function(messageArgs, level) {
410
412
  if (!Faye.logger) return;
411
-
413
+
412
414
  var levels = Faye.Logging.LOG_LEVELS;
413
415
  if (levels[Faye.Logging.logLevel] > levels[level]) return;
414
-
416
+
415
417
  var messageArgs = Array.prototype.slice.apply(messageArgs),
416
418
  banner = ' [' + level.toUpperCase() + '] [Faye',
417
419
  klass = this.className,
418
-
420
+
419
421
  message = messageArgs.shift().replace(/\?/g, function() {
420
422
  try {
421
423
  return Faye.toJSON(messageArgs.shift());
@@ -423,7 +425,7 @@ Faye.Logging = {
423
425
  return '[Object]';
424
426
  }
425
427
  });
426
-
428
+
427
429
  for (var key in Faye) {
428
430
  if (klass) continue;
429
431
  if (typeof Faye[key] !== 'function') continue;
@@ -431,7 +433,7 @@ Faye.Logging = {
431
433
  }
432
434
  if (klass) banner += '.' + klass;
433
435
  banner += '] ';
434
-
436
+
435
437
  Faye.logger(Faye.timestamp() + banner + message);
436
438
  }
437
439
  };
@@ -478,7 +480,7 @@ Faye.Extensible = {
478
480
  this._extensions.push(extension);
479
481
  if (extension.added) extension.added(this);
480
482
  },
481
-
483
+
482
484
  removeExtension: function(extension) {
483
485
  if (!this._extensions) return;
484
486
  var i = this._extensions.length;
@@ -488,19 +490,19 @@ Faye.Extensible = {
488
490
  if (extension.removed) extension.removed(this);
489
491
  }
490
492
  },
491
-
493
+
492
494
  pipeThroughExtensions: function(stage, message, callback, context) {
493
495
  this.debug('Passing through ? extensions: ?', stage, message);
494
496
 
495
497
  if (!this._extensions) return callback.call(context, message);
496
498
  var extensions = this._extensions.slice();
497
-
499
+
498
500
  var pipe = function(message) {
499
501
  if (!message) return callback.call(context, message);
500
-
502
+
501
503
  var extension = extensions.shift();
502
504
  if (!extension) return callback.call(context, message);
503
-
505
+
504
506
  if (extension[stage]) extension[stage](message, pipe);
505
507
  else pipe(message);
506
508
  };
@@ -514,11 +516,11 @@ Faye.Channel = Faye.Class({
514
516
  initialize: function(name) {
515
517
  this.id = this.name = name;
516
518
  },
517
-
519
+
518
520
  push: function(message) {
519
521
  this.trigger('message', message);
520
522
  },
521
-
523
+
522
524
  isUnused: function() {
523
525
  return this.countListeners('message') === 0;
524
526
  }
@@ -532,75 +534,75 @@ Faye.extend(Faye.Channel, {
532
534
  SUBSCRIBE: '/meta/subscribe',
533
535
  UNSUBSCRIBE: '/meta/unsubscribe',
534
536
  DISCONNECT: '/meta/disconnect',
535
-
537
+
536
538
  META: 'meta',
537
539
  SERVICE: 'service',
538
-
540
+
539
541
  expand: function(name) {
540
542
  var segments = this.parse(name),
541
543
  channels = ['/**', name];
542
-
544
+
543
545
  var copy = segments.slice();
544
546
  copy[copy.length - 1] = '*';
545
547
  channels.push(this.unparse(copy));
546
-
548
+
547
549
  for (var i = 1, n = segments.length; i < n; i++) {
548
550
  copy = segments.slice(0, i);
549
551
  copy.push('**');
550
552
  channels.push(this.unparse(copy));
551
553
  }
552
-
554
+
553
555
  return channels;
554
556
  },
555
-
557
+
556
558
  isValid: function(name) {
557
559
  return Faye.Grammar.CHANNEL_NAME.test(name) ||
558
560
  Faye.Grammar.CHANNEL_PATTERN.test(name);
559
561
  },
560
-
562
+
561
563
  parse: function(name) {
562
564
  if (!this.isValid(name)) return null;
563
565
  return name.split('/').slice(1);
564
566
  },
565
-
567
+
566
568
  unparse: function(segments) {
567
569
  return '/' + segments.join('/');
568
570
  },
569
-
571
+
570
572
  isMeta: function(name) {
571
573
  var segments = this.parse(name);
572
574
  return segments ? (segments[0] === this.META) : null;
573
575
  },
574
-
576
+
575
577
  isService: function(name) {
576
578
  var segments = this.parse(name);
577
579
  return segments ? (segments[0] === this.SERVICE) : null;
578
580
  },
579
-
581
+
580
582
  isSubscribable: function(name) {
581
583
  if (!this.isValid(name)) return null;
582
584
  return !this.isMeta(name) && !this.isService(name);
583
585
  },
584
-
586
+
585
587
  Set: Faye.Class({
586
588
  initialize: function() {
587
589
  this._channels = {};
588
590
  },
589
-
591
+
590
592
  getKeys: function() {
591
593
  var keys = [];
592
594
  for (var key in this._channels) keys.push(key);
593
595
  return keys;
594
596
  },
595
-
597
+
596
598
  remove: function(name) {
597
599
  delete this._channels[name];
598
600
  },
599
-
601
+
600
602
  hasSubscription: function(name) {
601
603
  return this._channels.hasOwnProperty(name);
602
604
  },
603
-
605
+
604
606
  subscribe: function(names, callback, context) {
605
607
  if (!callback) return;
606
608
  var name;
@@ -610,12 +612,12 @@ Faye.extend(Faye.Channel, {
610
612
  channel.bind('message', callback, context);
611
613
  }
612
614
  },
613
-
615
+
614
616
  unsubscribe: function(name, callback, context) {
615
617
  var channel = this._channels[name];
616
618
  if (!channel) return false;
617
619
  channel.unbind('message', callback, context);
618
-
620
+
619
621
  if (channel.isUnused()) {
620
622
  this.remove(name);
621
623
  return true;
@@ -623,10 +625,10 @@ Faye.extend(Faye.Channel, {
623
625
  return false;
624
626
  }
625
627
  },
626
-
628
+
627
629
  distributeMessage: function(message) {
628
630
  var channels = Faye.Channel.expand(message.channel);
629
-
631
+
630
632
  for (var i = 0, n = channels.length; i < n; i++) {
631
633
  var channel = this._channels[channels[i]];
632
634
  if (channel) channel.trigger('message', message.data);
@@ -647,13 +649,13 @@ Faye.Subscription = Faye.Class({
647
649
  this._context = context;
648
650
  this._cancelled = false;
649
651
  },
650
-
652
+
651
653
  cancel: function() {
652
654
  if (this._cancelled) return;
653
655
  this._client.unsubscribe(this._channels, this._callback, this._context);
654
656
  this._cancelled = true;
655
657
  },
656
-
658
+
657
659
  unsubscribe: function() {
658
660
  this.cancel();
659
661
  }
@@ -667,20 +669,20 @@ Faye.Client = Faye.Class({
667
669
  CONNECTING: 2,
668
670
  CONNECTED: 3,
669
671
  DISCONNECTED: 4,
670
-
672
+
671
673
  HANDSHAKE: 'handshake',
672
674
  RETRY: 'retry',
673
675
  NONE: 'none',
674
-
676
+
675
677
  CONNECTION_TIMEOUT: 60.0,
676
678
  DEFAULT_RETRY: 5.0,
677
-
679
+
678
680
  DEFAULT_ENDPOINT: '/bayeux',
679
681
  INTERVAL: 0.0,
680
-
682
+
681
683
  initialize: function(endpoint, options) {
682
684
  this.info('New client created for ?', endpoint);
683
-
685
+
684
686
  this._options = options || {};
685
687
  this.endpoint = endpoint || this.DEFAULT_ENDPOINT;
686
688
  this.endpoints = this._options.endpoints || {};
@@ -689,36 +691,34 @@ Faye.Client = Faye.Class({
689
691
  this._headers = {};
690
692
  this._disabled = [];
691
693
  this.retry = this._options.retry || this.DEFAULT_RETRY;
692
-
693
- this._selectTransport(Faye.MANDATORY_CONNECTION_TYPES);
694
-
694
+
695
695
  this._state = this.UNCONNECTED;
696
696
  this._channels = new Faye.Channel.Set();
697
697
  this._messageId = 0;
698
-
698
+
699
699
  this._responseCallbacks = {};
700
-
700
+
701
701
  this._advice = {
702
702
  reconnect: this.RETRY,
703
703
  interval: 1000 * (this._options.interval || this.INTERVAL),
704
704
  timeout: 1000 * (this._options.timeout || this.CONNECTION_TIMEOUT)
705
705
  };
706
-
706
+
707
707
  if (Faye.Event)
708
708
  Faye.Event.on(Faye.ENV, 'beforeunload', function() {
709
709
  if (Faye.indexOf(this._disabled, 'autodisconnect') < 0)
710
710
  this.disconnect();
711
711
  }, this);
712
712
  },
713
-
713
+
714
714
  disable: function(feature) {
715
715
  this._disabled.push(feature);
716
716
  },
717
-
717
+
718
718
  setHeader: function(name, value) {
719
719
  this._headers[name] = value;
720
720
  },
721
-
721
+
722
722
  getClientId: function() {
723
723
  return this._clientId;
724
724
  },
@@ -739,7 +739,7 @@ Faye.Client = Faye.Class({
739
739
  // MAY include: * minimumVersion
740
740
  // * ext
741
741
  // * id
742
- //
742
+ //
743
743
  // Success Response Failed Response
744
744
  // MUST include: * channel MUST include: * channel
745
745
  // * version * successful
@@ -754,33 +754,31 @@ Faye.Client = Faye.Class({
754
754
  handshake: function(callback, context) {
755
755
  if (this._advice.reconnect === this.NONE) return;
756
756
  if (this._state !== this.UNCONNECTED) return;
757
-
757
+
758
758
  this._state = this.CONNECTING;
759
759
  var self = this;
760
-
760
+
761
761
  this.info('Initiating handshake with ?', this.endpoint);
762
-
762
+ this._selectTransport(Faye.MANDATORY_CONNECTION_TYPES);
763
+
763
764
  this._send({
764
765
  channel: Faye.Channel.HANDSHAKE,
765
766
  version: Faye.BAYEUX_VERSION,
766
767
  supportedConnectionTypes: [this._transport.connectionType]
767
-
768
+
768
769
  }, function(response) {
769
-
770
+
770
771
  if (response.successful) {
771
772
  this._state = this.CONNECTED;
772
773
  this._clientId = response.clientId;
773
-
774
- var connectionTypes = Faye.filter(response.supportedConnectionTypes, function(connType) {
775
- return Faye.indexOf(this._disabled, connType) < 0;
776
- }, this);
777
- this._selectTransport(connectionTypes);
778
-
774
+
775
+ this._selectTransport(response.supportedConnectionTypes);
776
+
779
777
  this.info('Handshake successful: ?', this._clientId);
780
-
778
+
781
779
  this.subscribe(this._channels.getKeys(), true);
782
780
  if (callback) callback.call(context);
783
-
781
+
784
782
  } else {
785
783
  this.info('Handshake unsuccessful');
786
784
  Faye.ENV.setTimeout(function() { self.handshake(callback, context) }, this._advice.interval);
@@ -788,7 +786,7 @@ Faye.Client = Faye.Class({
788
786
  }
789
787
  }, this);
790
788
  },
791
-
789
+
792
790
  // Request Response
793
791
  // MUST include: * channel MUST include: * channel
794
792
  // * clientId * successful
@@ -801,30 +799,30 @@ Faye.Client = Faye.Class({
801
799
  connect: function(callback, context) {
802
800
  if (this._advice.reconnect === this.NONE) return;
803
801
  if (this._state === this.DISCONNECTED) return;
804
-
802
+
805
803
  if (this._state === this.UNCONNECTED)
806
804
  return this.handshake(function() { this.connect(callback, context) }, this);
807
-
805
+
808
806
  this.callback(callback, context);
809
807
  if (this._state !== this.CONNECTED) return;
810
-
808
+
811
809
  this.info('Calling deferred actions for ?', this._clientId);
812
810
  this.setDeferredStatus('succeeded');
813
811
  this.setDeferredStatus('deferred');
814
-
812
+
815
813
  if (this._connectRequest) return;
816
814
  this._connectRequest = true;
817
-
815
+
818
816
  this.info('Initiating connection for ?', this._clientId);
819
-
817
+
820
818
  this._send({
821
819
  channel: Faye.Channel.CONNECT,
822
820
  clientId: this._clientId,
823
821
  connectionType: this._transport.connectionType
824
-
822
+
825
823
  }, this._cycleConnection, this);
826
824
  },
827
-
825
+
828
826
  // Request Response
829
827
  // MUST include: * channel MUST include: * channel
830
828
  // * clientId * successful
@@ -835,21 +833,21 @@ Faye.Client = Faye.Class({
835
833
  disconnect: function() {
836
834
  if (this._state !== this.CONNECTED) return;
837
835
  this._state = this.DISCONNECTED;
838
-
836
+
839
837
  this.info('Disconnecting ?', this._clientId);
840
-
838
+
841
839
  this._send({
842
840
  channel: Faye.Channel.DISCONNECT,
843
841
  clientId: this._clientId
844
-
842
+
845
843
  }, function(response) {
846
844
  if (response.successful) this._transport.close();
847
845
  }, this);
848
-
846
+
849
847
  this.info('Clearing channel listeners for ?', this._clientId);
850
848
  this._channels = new Faye.Channel.Set();
851
849
  },
852
-
850
+
853
851
  // Request Response
854
852
  // MUST include: * channel MUST include: * channel
855
853
  // * clientId * successful
@@ -865,41 +863,41 @@ Faye.Client = Faye.Class({
865
863
  return Faye.map(channel, function(c) {
866
864
  return this.subscribe(c, callback, context);
867
865
  }, this);
868
-
866
+
869
867
  var subscription = new Faye.Subscription(this, channel, callback, context),
870
868
  force = (callback === true),
871
869
  hasSubscribe = this._channels.hasSubscription(channel);
872
-
870
+
873
871
  if (hasSubscribe && !force) {
874
872
  this._channels.subscribe([channel], callback, context);
875
873
  subscription.setDeferredStatus('succeeded');
876
874
  return subscription;
877
875
  }
878
-
876
+
879
877
  this.connect(function() {
880
878
  this.info('Client ? attempting to subscribe to ?', this._clientId, channel);
881
879
  if (!force) this._channels.subscribe([channel], callback, context);
882
-
880
+
883
881
  this._send({
884
882
  channel: Faye.Channel.SUBSCRIBE,
885
883
  clientId: this._clientId,
886
884
  subscription: channel
887
-
885
+
888
886
  }, function(response) {
889
887
  if (!response.successful) {
890
888
  subscription.setDeferredStatus('failed', Faye.Error.parse(response.error));
891
889
  return this._channels.unsubscribe(channel, callback, context);
892
890
  }
893
-
891
+
894
892
  var channels = [].concat(response.subscription);
895
893
  this.info('Subscription acknowledged for ? to ?', this._clientId, channels);
896
894
  subscription.setDeferredStatus('succeeded');
897
895
  }, this);
898
896
  }, this);
899
-
897
+
900
898
  return subscription;
901
899
  },
902
-
900
+
903
901
  // Request Response
904
902
  // MUST include: * channel MUST include: * channel
905
903
  // * clientId * successful
@@ -915,27 +913,27 @@ Faye.Client = Faye.Class({
915
913
  return Faye.map(channel, function(c) {
916
914
  return this.unsubscribe(c, callback, context);
917
915
  }, this);
918
-
916
+
919
917
  var dead = this._channels.unsubscribe(channel, callback, context);
920
918
  if (!dead) return;
921
-
919
+
922
920
  this.connect(function() {
923
921
  this.info('Client ? attempting to unsubscribe from ?', this._clientId, channel);
924
-
922
+
925
923
  this._send({
926
924
  channel: Faye.Channel.UNSUBSCRIBE,
927
925
  clientId: this._clientId,
928
926
  subscription: channel
929
-
927
+
930
928
  }, function(response) {
931
929
  if (!response.successful) return;
932
-
930
+
933
931
  var channels = [].concat(response.subscription);
934
932
  this.info('Unsubscription acknowledged for ? from ?', this._clientId, channels);
935
933
  }, this);
936
934
  }, this);
937
935
  },
938
-
936
+
939
937
  // Request Response
940
938
  // MUST include: * channel MUST include: * channel
941
939
  // * data * successful
@@ -944,10 +942,10 @@ Faye.Client = Faye.Class({
944
942
  // * ext * ext
945
943
  publish: function(channel, data) {
946
944
  var publication = new Faye.Publication();
947
-
945
+
948
946
  this.connect(function() {
949
947
  this.info('Client ? queueing published message to ?: ?', this._clientId, channel, data);
950
-
948
+
951
949
  this._send({
952
950
  channel: channel,
953
951
  data: data,
@@ -959,41 +957,44 @@ Faye.Client = Faye.Class({
959
957
  publication.setDeferredStatus('failed', Faye.Error.parse(response.error));
960
958
  }, this);
961
959
  }, this);
962
-
960
+
963
961
  return publication;
964
962
  },
965
-
963
+
966
964
  receiveMessage: function(message) {
967
965
  this.pipeThroughExtensions('incoming', message, function(message) {
968
966
  if (!message) return;
969
-
967
+
970
968
  if (message.advice) this._handleAdvice(message.advice);
971
969
  this._deliverMessage(message);
972
-
970
+
973
971
  if (message.successful === undefined) return;
974
-
972
+
975
973
  var callback = this._responseCallbacks[message.id];
976
974
  if (!callback) return;
977
-
975
+
978
976
  delete this._responseCallbacks[message.id];
979
977
  callback[0].call(callback[1], message);
980
978
  }, this);
981
979
  },
982
-
980
+
983
981
  _selectTransport: function(transportTypes) {
984
- Faye.Transport.get(this, transportTypes, function(transport) {
982
+ Faye.Transport.get(this, transportTypes, this._disabled, function(transport) {
985
983
  this.debug('Selected ? transport for ?', transport.connectionType, transport.endpoint);
986
-
984
+
985
+ if (transport === this._transport) return;
986
+ if (this._transport) this._transport.close();
987
+
987
988
  this._transport = transport;
988
989
  this._transport.cookies = this._cookies;
989
990
  this._transport.headers = this._headers;
990
-
991
+
991
992
  transport.bind('down', function() {
992
993
  if (this._transportUp !== undefined && !this._transportUp) return;
993
994
  this._transportUp = false;
994
995
  this.trigger('transport:down');
995
996
  }, this);
996
-
997
+
997
998
  transport.bind('up', function() {
998
999
  if (this._transportUp !== undefined && this._transportUp) return;
999
1000
  this._transportUp = true;
@@ -1001,7 +1002,7 @@ Faye.Client = Faye.Class({
1001
1002
  }, this);
1002
1003
  }, this);
1003
1004
  },
1004
-
1005
+
1005
1006
  _send: function(message, callback, context) {
1006
1007
  message.id = this._generateMessageId();
1007
1008
  if (callback) this._responseCallbacks[message.id] = [callback, context];
@@ -1011,7 +1012,7 @@ Faye.Client = Faye.Class({
1011
1012
  this._transport.send(message, this._advice.timeout / 1000);
1012
1013
  }, this);
1013
1014
  },
1014
-
1015
+
1015
1016
  _generateMessageId: function() {
1016
1017
  this._messageId += 1;
1017
1018
  if (this._messageId >= Math.pow(2,32)) this._messageId = 0;
@@ -1020,26 +1021,26 @@ Faye.Client = Faye.Class({
1020
1021
 
1021
1022
  _handleAdvice: function(advice) {
1022
1023
  Faye.extend(this._advice, advice);
1023
-
1024
+
1024
1025
  if (this._advice.reconnect === this.HANDSHAKE && this._state !== this.DISCONNECTED) {
1025
1026
  this._state = this.UNCONNECTED;
1026
1027
  this._clientId = null;
1027
1028
  this._cycleConnection();
1028
1029
  }
1029
1030
  },
1030
-
1031
+
1031
1032
  _deliverMessage: function(message) {
1032
1033
  if (!message.channel || message.data === undefined) return;
1033
1034
  this.info('Client ? calling listeners for ? with ?', this._clientId, message.channel, message.data);
1034
1035
  this._channels.distributeMessage(message);
1035
1036
  },
1036
-
1037
+
1037
1038
  _teardownConnection: function() {
1038
1039
  if (!this._connectRequest) return;
1039
1040
  this._connectRequest = null;
1040
1041
  this.info('Closed connection for ?', this._clientId);
1041
1042
  },
1042
-
1043
+
1043
1044
  _cycleConnection: function() {
1044
1045
  this._teardownConnection();
1045
1046
  var self = this;
@@ -1062,9 +1063,9 @@ Faye.Transport = Faye.extend(Faye.Class({
1062
1063
  this.endpoint = endpoint;
1063
1064
  this._outbox = [];
1064
1065
  },
1065
-
1066
+
1066
1067
  close: function() {},
1067
-
1068
+
1068
1069
  send: function(message, timeout) {
1069
1070
  this.debug('Client ? sending message to ?: ?',
1070
1071
  this._client._clientId, this.endpoint, message);
@@ -1093,48 +1094,50 @@ Faye.Transport = Faye.extend(Faye.Class({
1093
1094
  this._connectMessage.advice = {timeout: 0};
1094
1095
 
1095
1096
  this.request(this._outbox, this._timeout);
1096
-
1097
+
1097
1098
  this._connectMessage = null;
1098
1099
  this._outbox = [];
1099
1100
  },
1100
-
1101
+
1101
1102
  receive: function(responses) {
1102
1103
  this.debug('Client ? received from ?: ?',
1103
1104
  this._client._clientId, this.endpoint, responses);
1104
-
1105
+
1105
1106
  for (var i = 0, n = responses.length; i < n; i++) {
1106
1107
  this._client.receiveMessage(responses[i]);
1107
1108
  }
1108
1109
  },
1109
-
1110
+
1110
1111
  retry: function(message, timeout) {
1111
1112
  var called = false,
1112
1113
  retry = this._client.retry * 1000,
1113
1114
  self = this;
1114
-
1115
+
1115
1116
  return function() {
1116
1117
  if (called) return;
1117
1118
  called = true;
1118
1119
  Faye.ENV.setTimeout(function() { self.request(message, timeout) }, retry);
1119
1120
  };
1120
1121
  }
1121
-
1122
+
1122
1123
  }), {
1123
1124
  MAX_URL_LENGTH: 2048,
1124
-
1125
- get: function(client, connectionTypes, callback, context) {
1125
+
1126
+ get: function(client, allowed, disabled, callback, context) {
1126
1127
  var endpoint = client.endpoint;
1127
- if (connectionTypes === undefined) connectionTypes = this.supportedConnectionTypes();
1128
-
1128
+
1129
1129
  Faye.asyncEach(this._transports, function(pair, resume) {
1130
1130
  var connType = pair[0], klass = pair[1],
1131
1131
  connEndpoint = client.endpoints[connType] || endpoint;
1132
-
1133
- if (Faye.indexOf(connectionTypes, connType) < 0) {
1132
+
1133
+ if (Faye.indexOf(disabled, connType) >= 0)
1134
+ return resume();
1135
+
1136
+ if (Faye.indexOf(allowed, connType) < 0) {
1134
1137
  klass.isUsable(client, connEndpoint, function() {});
1135
1138
  return resume();
1136
1139
  }
1137
-
1140
+
1138
1141
  klass.isUsable(client, connEndpoint, function(isUsable) {
1139
1142
  if (!isUsable) return resume();
1140
1143
  var transport = klass.hasOwnProperty('create') ? klass.create(client, connEndpoint) : new klass(client, connEndpoint);
@@ -1144,17 +1147,13 @@ Faye.Transport = Faye.extend(Faye.Class({
1144
1147
  throw new Error('Could not find a usable connection type for ' + endpoint);
1145
1148
  });
1146
1149
  },
1147
-
1150
+
1148
1151
  register: function(type, klass) {
1149
1152
  this._transports.push([type, klass]);
1150
1153
  klass.prototype.connectionType = type;
1151
1154
  },
1152
-
1153
- _transports: [],
1154
-
1155
- supportedConnectionTypes: function() {
1156
- return Faye.map(this._transports, function(pair) { return pair[0] });
1157
- }
1155
+
1156
+ _transports: []
1158
1157
  });
1159
1158
 
1160
1159
  Faye.extend(Faye.Transport.prototype, Faye.Logging);
@@ -1164,15 +1163,15 @@ Faye.extend(Faye.Transport.prototype, Faye.Timeouts);
1164
1163
 
1165
1164
  Faye.Event = {
1166
1165
  _registry: [],
1167
-
1166
+
1168
1167
  on: function(element, eventName, callback, context) {
1169
1168
  var wrapped = function() { callback.call(context) };
1170
-
1169
+
1171
1170
  if (element.addEventListener)
1172
1171
  element.addEventListener(eventName, wrapped, false);
1173
1172
  else
1174
1173
  element.attachEvent('on' + eventName, wrapped);
1175
-
1174
+
1176
1175
  this._registry.push({
1177
1176
  _element: element,
1178
1177
  _type: eventName,
@@ -1181,23 +1180,23 @@ Faye.Event = {
1181
1180
  _handler: wrapped
1182
1181
  });
1183
1182
  },
1184
-
1183
+
1185
1184
  detach: function(element, eventName, callback, context) {
1186
1185
  var i = this._registry.length, register;
1187
1186
  while (i--) {
1188
1187
  register = this._registry[i];
1189
-
1188
+
1190
1189
  if ((element && element !== register._element) ||
1191
1190
  (eventName && eventName !== register._type) ||
1192
1191
  (callback && callback !== register._callback) ||
1193
1192
  (context && context !== register._context))
1194
1193
  continue;
1195
-
1194
+
1196
1195
  if (register._element.removeEventListener)
1197
1196
  register._element.removeEventListener(register._type, register._handler, false);
1198
1197
  else
1199
1198
  register._element.detachEvent('on' + register._type, register._handler);
1200
-
1199
+
1201
1200
  this._registry.splice(i,1);
1202
1201
  register = null;
1203
1202
  }
@@ -1216,17 +1215,17 @@ Faye.URI = Faye.extend(Faye.Class({
1216
1215
  }
1217
1216
  return pairs.join('&');
1218
1217
  },
1219
-
1218
+
1220
1219
  isSameOrigin: function() {
1221
- var host = Faye.URI.parse(Faye.ENV.location.href);
1222
-
1220
+ var host = Faye.URI.parse(Faye.ENV.location.href, false);
1221
+
1223
1222
  var external = (host.hostname !== this.hostname) ||
1224
1223
  (host.port !== this.port) ||
1225
1224
  (host.protocol !== this.protocol);
1226
-
1225
+
1227
1226
  return !external;
1228
1227
  },
1229
-
1228
+
1230
1229
  toURL: function() {
1231
1230
  var query = this.queryString();
1232
1231
  return this.protocol + '//' + this.hostname + (this.port ? ':' + this.port : '') +
@@ -1236,7 +1235,7 @@ Faye.URI = Faye.extend(Faye.Class({
1236
1235
  parse: function(url, params) {
1237
1236
  if (typeof url !== 'string') return url;
1238
1237
  var uri = new this(), parts;
1239
-
1238
+
1240
1239
  var consume = function(name, pattern, infer) {
1241
1240
  url = url.replace(pattern, function(match) {
1242
1241
  uri[name] = match;
@@ -1245,15 +1244,15 @@ Faye.URI = Faye.extend(Faye.Class({
1245
1244
  if (uri[name] === undefined)
1246
1245
  uri[name] = infer ? Faye.ENV.location[name] : '';
1247
1246
  };
1248
-
1247
+
1249
1248
  consume('protocol', /^https?\:/, true);
1250
1249
  consume('host', /^\/\/[^\/]+/, true);
1251
-
1250
+
1252
1251
  if (!/^\//.test(url)) url = Faye.ENV.location.pathname.replace(/[^\/]*$/, '') + url;
1253
1252
  consume('pathname', /^\/[^\?#]*/);
1254
1253
  consume('search', /^\?[^#]*/);
1255
1254
  consume('hash', /^#.*/);
1256
-
1255
+
1257
1256
  if (/^\/\//.test(uri.host)) {
1258
1257
  uri.host = uri.host.substr(2);
1259
1258
  parts = uri.host.split(':');
@@ -1263,20 +1262,24 @@ Faye.URI = Faye.extend(Faye.Class({
1263
1262
  uri.hostname = Faye.ENV.location.hostname;
1264
1263
  uri.port = Faye.ENV.location.port;
1265
1264
  }
1266
-
1267
- var query = uri.search.replace(/^\?/, ''),
1268
- pairs = query ? query.split('&') : [],
1269
- n = pairs.length,
1270
- data = {};
1271
-
1272
- while (n--) {
1273
- parts = pairs[n].split('=');
1274
- data[decodeURIComponent(parts[0] || '')] = decodeURIComponent(parts[1] || '');
1265
+
1266
+ if (params === false) {
1267
+ uri.params = {};
1268
+ } else {
1269
+ var query = uri.search.replace(/^\?/, ''),
1270
+ pairs = query ? query.split('&') : [],
1271
+ n = pairs.length,
1272
+ data = {};
1273
+
1274
+ while (n--) {
1275
+ parts = pairs[n].split('=');
1276
+ data[decodeURIComponent(parts[0] || '')] = decodeURIComponent(parts[1] || '');
1277
+ }
1278
+ if (typeof params === 'object') Faye.extend(data, params);
1279
+
1280
+ uri.params = data;
1275
1281
  }
1276
- if (typeof params === 'object') Faye.extend(data, params);
1277
-
1278
- uri.params = data;
1279
-
1282
+
1280
1283
  return uri;
1281
1284
  }
1282
1285
  });
@@ -1773,75 +1776,81 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
1773
1776
  CONNECTED: 3,
1774
1777
 
1775
1778
  batching: false,
1776
-
1779
+
1777
1780
  isUsable: function(callback, context) {
1778
1781
  this.callback(function() { callback.call(context, true) });
1779
1782
  this.errback(function() { callback.call(context, false) });
1780
1783
  this.connect();
1781
1784
  },
1782
-
1785
+
1783
1786
  request: function(messages, timeout) {
1784
1787
  if (messages.length === 0) return;
1785
1788
  this._messages = this._messages || {};
1786
-
1789
+
1787
1790
  for (var i = 0, n = messages.length; i < n; i++) {
1788
1791
  this._messages[messages[i].id] = messages[i];
1789
1792
  }
1790
1793
  this.callback(function(socket) { socket.send(Faye.toJSON(messages)) });
1791
1794
  this.connect();
1792
1795
  },
1793
-
1796
+
1794
1797
  close: function() {
1795
- if (this._closed) return;
1796
- this._closed = true;
1797
- if (this._socket) this._socket.close();
1798
+ if (!this._socket) return;
1799
+ this._socket.onclose = this._socket.onerror = null;
1800
+ this._socket.close();
1801
+ delete this._socket;
1802
+ this.setDeferredStatus('deferred');
1803
+ this._state = this.UNCONNECTED;
1798
1804
  },
1799
-
1805
+
1800
1806
  connect: function() {
1801
1807
  if (Faye.Transport.WebSocket._unloaded) return;
1802
- if (this._closed) return;
1803
-
1808
+
1804
1809
  this._state = this._state || this.UNCONNECTED;
1805
1810
  if (this._state !== this.UNCONNECTED) return;
1806
-
1811
+
1807
1812
  this._state = this.CONNECTING;
1808
-
1813
+
1809
1814
  var ws = Faye.Transport.WebSocket.getClass();
1810
1815
  if (!ws) return this.setDeferredStatus('failed');
1811
-
1816
+
1812
1817
  this._socket = new ws(Faye.Transport.WebSocket.getSocketUrl(this.endpoint));
1813
1818
  var self = this;
1814
-
1819
+
1815
1820
  this._socket.onopen = function() {
1816
1821
  self._state = self.CONNECTED;
1817
1822
  self._everConnected = true;
1818
1823
  self.setDeferredStatus('succeeded', self._socket);
1819
1824
  self.trigger('up');
1820
1825
  };
1821
-
1826
+
1822
1827
  this._socket.onmessage = function(event) {
1823
- var messages = [].concat(JSON.parse(event.data));
1828
+ var messages = JSON.parse(event.data);
1829
+ if (!messages) return;
1830
+ messages = [].concat(messages);
1831
+
1824
1832
  for (var i = 0, n = messages.length; i < n; i++) {
1825
1833
  delete self._messages[messages[i].id];
1826
1834
  }
1827
1835
  self.receive(messages);
1828
1836
  };
1829
-
1830
- this._socket.onclose = function() {
1837
+
1838
+ this._socket.onclose = this._socket.onerror = function() {
1831
1839
  var wasConnected = (self._state === self.CONNECTED);
1832
1840
  self.setDeferredStatus('deferred');
1833
1841
  self._state = self.UNCONNECTED;
1834
- delete self._socket;
1835
-
1842
+
1843
+ self.close();
1844
+
1836
1845
  if (wasConnected) return self.resend();
1837
1846
  if (!self._everConnected) return self.setDeferredStatus('failed');
1838
-
1847
+
1839
1848
  var retry = self._client.retry * 1000;
1840
1849
  Faye.ENV.setTimeout(function() { self.connect() }, retry);
1841
1850
  self.trigger('down');
1842
1851
  };
1843
1852
  },
1844
-
1853
+
1845
1854
  resend: function() {
1846
1855
  if (!this._messages) return;
1847
1856
  var messages = Faye.map(this._messages, function(id, msg) { return msg });
@@ -1852,17 +1861,17 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
1852
1861
  if (Faye.URI) endpoint = Faye.URI.parse(endpoint).toURL();
1853
1862
  return endpoint.replace(/^http(s?):/ig, 'ws$1:');
1854
1863
  },
1855
-
1864
+
1856
1865
  getClass: function() {
1857
1866
  return (Faye.WebSocket && Faye.WebSocket.Client) ||
1858
1867
  Faye.ENV.WebSocket ||
1859
1868
  Faye.ENV.MozWebSocket;
1860
1869
  },
1861
-
1870
+
1862
1871
  isUsable: function(client, endpoint, callback, context) {
1863
1872
  this.create(client, endpoint).isUsable(callback, context);
1864
1873
  },
1865
-
1874
+
1866
1875
  create: function(client, endpoint) {
1867
1876
  var sockets = client.transports.websocket = client.transports.websocket || {};
1868
1877
  sockets[endpoint] = sockets[endpoint] || new this(client, endpoint);
@@ -1883,18 +1892,18 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
1883
1892
  initialize: function(client, endpoint) {
1884
1893
  Faye.Transport.prototype.initialize.call(this, client, endpoint);
1885
1894
  if (!Faye.ENV.EventSource) return this.setDeferredStatus('failed');
1886
-
1895
+
1887
1896
  this._xhr = new Faye.Transport.XHR(client, endpoint);
1888
-
1897
+
1889
1898
  var socket = new EventSource(endpoint + '/' + client.getClientId()),
1890
1899
  self = this;
1891
-
1900
+
1892
1901
  socket.onopen = function() {
1893
1902
  self._everConnected = true;
1894
1903
  self.setDeferredStatus('succeeded');
1895
1904
  self.trigger('up');
1896
1905
  };
1897
-
1906
+
1898
1907
  socket.onerror = function() {
1899
1908
  if (self._everConnected) {
1900
1909
  self.trigger('down');
@@ -1903,40 +1912,46 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
1903
1912
  socket.close();
1904
1913
  }
1905
1914
  };
1906
-
1915
+
1907
1916
  socket.onmessage = function(event) {
1908
1917
  self.receive(JSON.parse(event.data));
1909
1918
  self.trigger('up');
1910
1919
  };
1911
-
1920
+
1912
1921
  this._socket = socket;
1913
1922
  },
1914
-
1923
+
1915
1924
  isUsable: function(callback, context) {
1916
1925
  this.callback(function() { callback.call(context, true) });
1917
1926
  this.errback(function() { callback.call(context, false) });
1918
1927
  },
1919
-
1928
+
1920
1929
  request: function(message, timeout) {
1921
1930
  this._xhr.request(message, timeout);
1922
1931
  },
1923
-
1932
+
1924
1933
  close: function() {
1934
+ if (!this._socket) return;
1935
+ this._socket.onerror = null;
1925
1936
  this._socket.close();
1937
+ delete this._socket;
1926
1938
  }
1927
1939
  }), {
1928
1940
  isUsable: function(client, endpoint, callback, context) {
1929
1941
  var id = client.getClientId();
1930
1942
  if (!id) return callback.call(context, false);
1931
-
1943
+
1932
1944
  Faye.Transport.XHR.isUsable(client, endpoint, function(usable) {
1933
1945
  if (!usable) return callback.call(context, false);
1934
1946
  this.create(client, endpoint).isUsable(callback, context);
1935
1947
  }, this);
1936
1948
  },
1937
-
1949
+
1938
1950
  create: function(client, endpoint) {
1939
- var sockets = client.transports.eventsource = client.transports.eventsource || {};
1951
+ var sockets = client.transports.eventsource = client.transports.eventsource || {},
1952
+ id = client.getClientId(),
1953
+ endpoint = endpoint + '/' + (id || '');
1954
+
1940
1955
  sockets[endpoint] = sockets[endpoint] || new this(client, endpoint);
1941
1956
  return sockets[endpoint];
1942
1957
  }
@@ -1954,48 +1969,48 @@ Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, {
1954
1969
  xhr = Faye.ENV.ActiveXObject
1955
1970
  ? new ActiveXObject("Microsoft.XMLHTTP")
1956
1971
  : new XMLHttpRequest();
1957
-
1972
+
1958
1973
  xhr.open('POST', path, true);
1959
1974
  xhr.setRequestHeader('Content-Type', 'application/json');
1960
1975
  xhr.setRequestHeader('Pragma', 'no-cache');
1961
1976
  xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
1962
-
1977
+
1963
1978
  var headers = this.headers;
1964
1979
  for (var key in headers) {
1965
1980
  if (!headers.hasOwnProperty(key)) continue;
1966
1981
  xhr.setRequestHeader(key, headers[key]);
1967
1982
  }
1968
-
1983
+
1969
1984
  var abort = function() { xhr.abort() };
1970
1985
  Faye.Event.on(Faye.ENV, 'beforeunload', abort);
1971
-
1986
+
1972
1987
  var cleanUp = function() {
1973
1988
  Faye.Event.detach(Faye.ENV, 'beforeunload', abort);
1974
1989
  xhr.onreadystatechange = function() {};
1975
1990
  xhr = null;
1976
1991
  };
1977
-
1992
+
1978
1993
  xhr.onreadystatechange = function() {
1979
1994
  if (xhr.readyState !== 4) return;
1980
-
1995
+
1981
1996
  var parsedMessage = null,
1982
1997
  status = xhr.status,
1983
1998
  successful = ((status >= 200 && status < 300) ||
1984
1999
  status === 304 ||
1985
2000
  status === 1223);
1986
-
2001
+
1987
2002
  if (!successful) {
1988
2003
  cleanUp();
1989
2004
  retry();
1990
2005
  return self.trigger('down');
1991
2006
  }
1992
-
2007
+
1993
2008
  try {
1994
2009
  parsedMessage = JSON.parse(xhr.responseText);
1995
2010
  } catch (e) {}
1996
-
2011
+
1997
2012
  cleanUp();
1998
-
2013
+
1999
2014
  if (parsedMessage) {
2000
2015
  self.receive(parsedMessage);
2001
2016
  self.trigger('up');
@@ -2021,10 +2036,10 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2021
2036
  xhr = new xhrClass(),
2022
2037
  retry = this.retry(message, timeout),
2023
2038
  self = this;
2024
-
2039
+
2025
2040
  xhr.open('POST', this.endpoint, true);
2026
2041
  if (xhr.setRequestHeader) xhr.setRequestHeader('Pragma', 'no-cache');
2027
-
2042
+
2028
2043
  var cleanUp = function() {
2029
2044
  if (!xhr) return false;
2030
2045
  xhr.onload = xhr.onerror = xhr.ontimeout = xhr.onprogress = null;
@@ -2032,15 +2047,15 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2032
2047
  Faye.ENV.clearTimeout(timer);
2033
2048
  return true;
2034
2049
  };
2035
-
2050
+
2036
2051
  xhr.onload = function() {
2037
2052
  var parsedMessage = null;
2038
2053
  try {
2039
2054
  parsedMessage = JSON.parse(xhr.responseText);
2040
2055
  } catch (e) {}
2041
-
2056
+
2042
2057
  cleanUp();
2043
-
2058
+
2044
2059
  if (parsedMessage) {
2045
2060
  self.receive(parsedMessage);
2046
2061
  self.trigger('up');
@@ -2049,7 +2064,7 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2049
2064
  self.trigger('down');
2050
2065
  }
2051
2066
  };
2052
-
2067
+
2053
2068
  var onerror = function() {
2054
2069
  cleanUp();
2055
2070
  retry();
@@ -2058,7 +2073,7 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2058
2073
  var timer = Faye.ENV.setTimeout(onerror, 1.5 * 1000 * timeout);
2059
2074
  xhr.onerror = onerror;
2060
2075
  xhr.ontimeout = onerror;
2061
-
2076
+
2062
2077
  xhr.onprogress = function() {};
2063
2078
  xhr.send('message=' + encodeURIComponent(Faye.toJSON(message)));
2064
2079
  }
@@ -2066,11 +2081,11 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2066
2081
  isUsable: function(client, endpoint, callback, context) {
2067
2082
  if (Faye.URI.parse(endpoint).isSameOrigin())
2068
2083
  return callback.call(context, false);
2069
-
2084
+
2070
2085
  if (Faye.ENV.XDomainRequest)
2071
2086
  return callback.call(context, Faye.URI.parse(endpoint).protocol ===
2072
2087
  Faye.URI.parse(Faye.ENV.location).protocol);
2073
-
2088
+
2074
2089
  if (Faye.ENV.XMLHttpRequest) {
2075
2090
  var xhr = new Faye.ENV.XMLHttpRequest();
2076
2091
  return callback.call(context, xhr.withCredentials !== undefined);
@@ -2091,7 +2106,7 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2091
2106
  var location = Faye.URI.parse(this.endpoint, params).toURL();
2092
2107
  return location.length >= Faye.Transport.MAX_URL_LENGTH;
2093
2108
  },
2094
-
2109
+
2095
2110
  request: function(messages, timeout) {
2096
2111
  var params = {message: Faye.toJSON(messages)},
2097
2112
  head = document.getElementsByTagName('head')[0],
@@ -2100,19 +2115,19 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2100
2115
  location = Faye.URI.parse(this.endpoint, params),
2101
2116
  retry = this.retry(messages, timeout),
2102
2117
  self = this;
2103
-
2118
+
2104
2119
  Faye.ENV[callbackName] = function(data) {
2105
2120
  cleanUp();
2106
2121
  self.receive(data);
2107
2122
  self.trigger('up');
2108
2123
  };
2109
-
2124
+
2110
2125
  var timer = Faye.ENV.setTimeout(function() {
2111
2126
  cleanUp();
2112
2127
  retry();
2113
2128
  self.trigger('down');
2114
2129
  }, 1.5 * 1000 * timeout);
2115
-
2130
+
2116
2131
  var cleanUp = function() {
2117
2132
  if (!Faye.ENV[callbackName]) return false;
2118
2133
  Faye.ENV[callbackName] = undefined;
@@ -2121,7 +2136,7 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2121
2136
  script.parentNode.removeChild(script);
2122
2137
  return true;
2123
2138
  };
2124
-
2139
+
2125
2140
  location.params.jsonp = callbackName;
2126
2141
  script.type = 'text/javascript';
2127
2142
  script.src = location.toURL();
@@ -2129,12 +2144,12 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2129
2144
  }
2130
2145
  }), {
2131
2146
  _cbCount: 0,
2132
-
2147
+
2133
2148
  getCallbackName: function() {
2134
2149
  this._cbCount += 1;
2135
2150
  return '__jsonp' + this._cbCount + '__';
2136
2151
  },
2137
-
2152
+
2138
2153
  isUsable: function(client, endpoint, callback, context) {
2139
2154
  callback.call(context, true);
2140
2155
  }