diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..69af001 --- /dev/null +++ b/Guardfile @@ -0,0 +1,14 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +guard 'bundler' do + watch('Gemfile') + watch(/^.+\.gemspec/) +end + +guard 'rspec', :version => 2 do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } +end + diff --git a/faye-rails.gemspec b/faye-rails.gemspec index c6e3006..81b1b10 100644 --- a/faye-rails.gemspec +++ b/faye-rails.gemspec @@ -12,14 +12,21 @@ Gem::Specification.new do |s| s.summary = "Faye bindings for Rails 3.1+." s.license = 'MIT' - s.add_dependency "faye", ["~> 0.8.2"] + s.add_dependency "faye", ["~> 0.8.5"] + s.add_dependency 'eventmachine', ['~> 1.0.0'] s.add_development_dependency "rails", ["~> 3.1"] s.add_development_dependency "sqlite3" s.add_development_dependency "rspec" + s.add_development_dependency 'rspec-core' + s.add_development_dependency 'rspec-mocks' + s.add_development_dependency 'rspec-expectations' s.add_development_dependency "rspec-rails" s.add_development_dependency "database_cleaner" s.add_development_dependency "mocha" s.add_development_dependency "thin" + s.add_development_dependency "guard-rspec" + s.add_development_dependency "guard-bundler" + s.add_development_dependency 'rb-fsevent', ['~> 0.9.1'] s.files = %w(README.md) + Dir["lib/**/*", "vendor/**/*"] diff --git a/lib/faye-rails/controller/channel.rb b/lib/faye-rails/controller/channel.rb index 57ebf83..5e7482d 100644 --- a/lib/faye-rails/controller/channel.rb +++ b/lib/faye-rails/controller/channel.rb @@ -14,7 +14,7 @@ def client end def publish(message) - FayeRails.client(endpoint).publish(channel, message) + client.publish(channel, message) end def monitor(event, &block) diff --git a/spec/lib/faye-rails/controller/channel_spec.rb b/spec/lib/faye-rails/controller/channel_spec.rb new file mode 100644 index 0000000..7abcb74 --- /dev/null +++ b/spec/lib/faye-rails/controller/channel_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe FayeRails::Controller::Channel do + + let(:channel) { stub(:channel) } + let(:endpoint) { stub(:endpoint) } + let(:client) { stub(:client) } + subject { FayeRails::Controller::Channel.new(channel,endpoint) } + + describe '#client' do + example do + FayeRails.should_receive(:client).with(endpoint) + subject.client + end + end + + describe '#publish' do + example do + subject.should_receive(:client).and_return(client) + client.should_receive(:publish).with(channel, "Hello bob") + subject.publish("Hello bob") + end + end + + describe "#monitor" do + it "raises ArgumentError for unknown event" do + expect { subject.monitor(:blarg) }.to raise_error(ArgumentError, /^Unknown event/) + end + + example do + end + end + +end diff --git a/spec/controller_spec.rb b/spec/lib/faye-rails/controller_spec.rb similarity index 100% rename from spec/controller_spec.rb rename to spec/lib/faye-rails/controller_spec.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fa71f86..8557bed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,7 +24,8 @@ # config.mock_with :mocha # config.mock_with :flexmock # config.mock_with :rr - config.mock_with :mocha + #config.mock_with :mocha + config.mock_with :rspec # Include the Within Module config.include(Within) diff --git a/vendor/assets/javascripts/faye-browser-min.js b/vendor/assets/javascripts/faye-browser-min.js index 99f7fc8..c9d28e7 100644 --- a/vendor/assets/javascripts/faye-browser-min.js +++ b/vendor/assets/javascripts/faye-browser-min.js @@ -1,2 +1,2 @@ -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.3',BAYEUX_VERSION:'1.0',ID_LENGTH:128,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);return d}var f=Math.pow(2,a)-1,g=f.toString(36).length,d=Math.floor(Math.random()*f).toString(36);while(d.length0)i();j=false};var n=function(){k+=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(),k=b.getMinutes(),j=b.getSeconds();var i=function(a){return a<10?'0'+a:String(a)};return i(d)+'-'+i(f)+'-'+i(g)+' '+i(h)+':'+i(k)+':'+i(j)}});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._d={}},exists:function(a){return this._d.hasOwnProperty(a)},generate:function(){var a=Faye.random();while(this._d.hasOwnProperty(a))a=Faye.random();return this._d[a]=a},release:function(a){delete this._d[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._k);this._l=this._l||[];this._l.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._k);this._m=this._m||[];this._m.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._k=a;if(b==='succeeded')d=this._l;else if(b==='failed')d=this._m;if(!d)return;var f;while(f=d.shift())f[0].apply(f[1],this._k)}};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;gd[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 k in Faye){if(g)continue;if(typeof Faye[k]!=='function')continue;if(this instanceof Faye[k])g=k}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 k=this._6.slice();var j=function(a){if(!a)return g.call(h,a);var b=k.shift();if(!b)return g.call(h,a);if(b[d])b[d](a,j);else j(a)};j(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=Math.pow(2,32))this._f=0;return this._f.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._A()}},_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._r)return;this._r=null;this.info('Closed connection for ?',this._0)},_A: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.debug('Created new ? transport for ?',this.connectionType,b);this._7=a;this._b=b;this._g=[]},close:function(){},send:function(a,b){this.debug('Client ? sending message to ?: ?',this._7._0,this._b,a);if(!this.batching)return this.request([a],b);this._g.push(a);this._J=b;if(a.channel===Faye.Channel.HANDSHAKE)return this.flush();if(a.channel===Faye.Channel.CONNECT)this._s=a;this.addTimeout('publish',this.MAX_DELAY,this.flush,this)},flush:function(){this.removeTimeout('publish');if(this._g.length>1&&this._s)this._s.advice={timeout:0};this.request(this._g,this._J);this._s=null;this._g=[]},receive:function(a){this.debug('Client ? received from ?: ?',this._7._0,this._b,a);for(var b=0,d=a.length;b=200&&b<300)||b===304||b===1223);if(!d){m();h();return j.trigger('down')}try{a=JSON.parse(i.responseText)}catch(e){}m();if(a){j.receive(a);j.trigger('up')}else{h();j.trigger('down')}};i.send(Faye.toJSON(f))}}),{isUsable:function(a,b,d){b.call(d,Faye.URI.parse(a).isLocal())}});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),k=this;g.open('POST',this._b,true);var j=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){}j();if(a){k.receive(a);k.trigger('up')}else{h();k.trigger('down')}};var i=function(){j();h();k.trigger('down')};var l=Faye.ENV.setTimeout(i,1.5*1000*d);g.onerror=i;g.ontimeout=i;g.onprogress=function(){};g.send('message='+encodeURIComponent(Faye.toJSON(b)))}}),{isUsable:function(a,b,d){if(Faye.URI.parse(a).isLocal())return b.call(d,false);if(Faye.ENV.XDomainRequest)return b.call(d,Faye.URI.parse(a).protocol===Faye.URI.parse(Faye.ENV.location).protocol);if(Faye.ENV.XMLHttpRequest){var f=new Faye.ENV.XMLHttpRequest();return b.call(d,f.withCredentials!==undefined)}return b.call(d,false)}});Faye.Transport.register('cross-origin-long-polling',Faye.Transport.CORS);Faye.Transport.JSONP=Faye.extend(Faye.Class(Faye.Transport,{request:function(b,d){var f={message:Faye.toJSON(b)},g=document.getElementsByTagName('head')[0],h=document.createElement('script'),k=Faye.Transport.JSONP.getCallbackName(),j=Faye.URI.parse(this._b,f),i=this.retry(b,d),l=this;Faye.ENV[k]=function(a){o();l.receive(a);l.trigger('up')};var n=Faye.ENV.setTimeout(function(){o();i();l.trigger('down')},1.5*1000*d);var o=function(){if(!Faye.ENV[k])return false;Faye.ENV[k]=undefined;try{delete Faye.ENV[k]}catch(e){}Faye.ENV.clearTimeout(n);h.parentNode.removeChild(h);return true};j.params.jsonp=k;h.type='text/javascript';h.src=j.toURL();g.appendChild(h)}}),{_D:0,getCallbackName:function(){this._D+=1;return'__jsonp'+this._D+'__'},isUsable:function(a,b,d){b.call(d,true)}});Faye.Transport.register('callback-polling',Faye.Transport.JSONP); +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.5',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.length0)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;gd[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=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=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); //@ sourceMappingURL=faye-browser-min.js.map \ No newline at end of file diff --git a/vendor/assets/javascripts/faye-browser.js b/vendor/assets/javascripts/faye-browser.js index 58d1aad..6a27e42 100644 --- a/vendor/assets/javascripts/faye-browser.js +++ b/vendor/assets/javascripts/faye-browser.js @@ -13,10 +13,10 @@ Faye.extend = function(dest, source, overwrite) { }; Faye.extend(Faye, { - VERSION: '0.8.3', + VERSION: '0.8.5', BAYEUX_VERSION: '1.0', - ID_LENGTH: 128, + ID_LENGTH: 160, JSONP_CALLBACK: 'jsonpcallback', CONNECTION_TYPES: ['long-polling', 'cross-origin-long-polling', 'callback-polling', 'websocket', 'eventsource', 'in-process'], @@ -30,7 +30,9 @@ Faye.extend(Faye, { var parts = Math.ceil(bitlength / 32), string = ''; while (parts--) string += this.random(32); - return string; + var chars = string.split(''), result = ''; + while (chars.length > 0) result += chars.pop(); + return result; } var limit = Math.pow(2, bitlength) - 1, maxSize = limit.toString(36).length, @@ -718,6 +720,7 @@ Faye.Client = Faye.Class({ this._options = options || {}; this.endpoint = endpoint || this.DEFAULT_ENDPOINT; this.endpoints = this._options.endpoints || {}; + this.transports = {}; this._cookies = Faye.CookieJar && new Faye.CookieJar(); this._headers = {}; this._disabled = []; @@ -1015,6 +1018,8 @@ Faye.Client = Faye.Class({ _selectTransport: function(transportTypes) { Faye.Transport.get(this, transportTypes, function(transport) { + this.debug('Selected ? transport for ?', transport.connectionType, transport.endpoint); + this._transport = transport; this._transport.cookies = this._cookies; this._transport.headers = this._headers; @@ -1089,17 +1094,16 @@ Faye.Transport = Faye.extend(Faye.Class({ batching: true, initialize: function(client, endpoint) { - this.debug('Created new ? transport for ?', this.connectionType, endpoint); - this._client = client; - this._endpoint = endpoint; - this._outbox = []; + this._client = client; + this.endpoint = endpoint; + this._outbox = []; }, close: function() {}, send: function(message, timeout) { this.debug('Client ? sending message to ?: ?', - this._client._clientId, this._endpoint, message); + this._client._clientId, this.endpoint, message); if (!this.batching) return this.request([message], timeout); @@ -1107,11 +1111,14 @@ Faye.Transport = Faye.extend(Faye.Class({ this._timeout = timeout; if (message.channel === Faye.Channel.HANDSHAKE) - return this.flush(); + return this.addTimeout('publish', 0.01, this.flush, this); if (message.channel === Faye.Channel.CONNECT) this._connectMessage = message; + if (this.shouldFlush && this.shouldFlush(this._outbox)) + return this.flush(); + this.addTimeout('publish', this.MAX_DELAY, this.flush, this); }, @@ -1129,7 +1136,7 @@ Faye.Transport = Faye.extend(Faye.Class({ receive: function(responses) { this.debug('Client ? received from ?: ?', - this._client._clientId, this._endpoint, responses); + this._client._clientId, this.endpoint, responses); for (var i = 0, n = responses.length; i < n; i++) { this._client.receiveMessage(responses[i]); @@ -1149,6 +1156,8 @@ Faye.Transport = Faye.extend(Faye.Class({ } }), { + MAX_URL_LENGTH: 2048, + get: function(client, connectionTypes, callback, context) { var endpoint = client.endpoint; if (connectionTypes === undefined) connectionTypes = this.supportedConnectionTypes(); @@ -1157,11 +1166,15 @@ Faye.Transport = Faye.extend(Faye.Class({ var connType = pair[0], klass = pair[1], connEndpoint = client.endpoints[connType] || endpoint; - if (Faye.indexOf(connectionTypes, connType) < 0) return resume(); + if (Faye.indexOf(connectionTypes, connType) < 0) { + klass.isUsable(client, connEndpoint, function() {}); + return resume(); + } - klass.isUsable(connEndpoint, function(isUsable) { - if (isUsable) callback.call(context, new klass(client, connEndpoint)); - else resume(); + klass.isUsable(client, connEndpoint, function(isUsable) { + if (!isUsable) return resume(); + var transport = klass.hasOwnProperty('create') ? klass.create(client, connEndpoint) : new klass(client, connEndpoint); + callback.call(context, transport); }); }, function() { throw new Error('Could not find a usable connection type for ' + endpoint); @@ -1240,7 +1253,7 @@ Faye.URI = Faye.extend(Faye.Class({ return pairs.join('&'); }, - isLocal: function() { + isSameOrigin: function() { var host = Faye.URI.parse(Faye.ENV.location.href); var external = (host.hostname !== this.hostname) || @@ -1252,32 +1265,45 @@ Faye.URI = Faye.extend(Faye.Class({ toURL: function() { var query = this.queryString(); - return this.protocol + this.hostname + (this.port ? ':' + this.port : '') + - this.pathname + (query ? '?' + query : ''); + return this.protocol + '//' + this.hostname + (this.port ? ':' + this.port : '') + + this.pathname + (query ? '?' + query : '') + this.hash; } }), { parse: function(url, params) { if (typeof url !== 'string') return url; + var uri = new this(), parts; - var a = document.createElement('a'), - uri = new this(); + var consume = function(name, pattern, infer) { + url = url.replace(pattern, function(match) { + uri[name] = match; + return ''; + }); + if (uri[name] === undefined) + uri[name] = infer ? Faye.ENV.location[name] : ''; + }; - a.href = url; + consume('protocol', /^https?\:/, true); + consume('host', /^\/\/[^\/]+/, true); - uri.protocol = a.protocol + '//'; - uri.hostname = a.hostname; - uri.pathname = a.pathname.replace(/^\/?/, '/'); + if (!/^\//.test(url)) url = Faye.ENV.location.pathname.replace(/[^\/]*$/, '') + url; + consume('pathname', /^\/[^\?#]*/); + consume('search', /^\?[^#]*/); + consume('hash', /^#.*/); - if (a.port === '0' || a.port === '') - uri.port = (a.protocol === 'https:') ? '443' : '80'; - else - uri.port = a.port; + if (/^\/\//.test(uri.host)) { + uri.host = uri.host.substr(2); + parts = uri.host.split(':'); + uri.hostname = parts[0]; + uri.port = parts[1] || ''; + } else { + uri.hostname = Faye.ENV.location.hostname; + uri.port = Faye.ENV.location.port; + } - var query = a.search.replace(/^\?/, ''), + var query = uri.search.replace(/^\?/, ''), pairs = query ? query.split('&') : [], n = pairs.length, - data = {}, - parts; + data = {}; while (n--) { parts = pairs[n].split('='); @@ -1784,6 +1810,12 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, { batching: false, + isUsable: function(callback, context) { + this.callback(function() { callback.call(context, true) }); + this.errback(function() { callback.call(context, false) }); + this.connect(); + }, + request: function(messages, timeout) { if (messages.length === 0) return; this._messages = this._messages || {}; @@ -1791,11 +1823,7 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, { for (var i = 0, n = messages.length; i < n; i++) { this._messages[messages[i].id] = messages[i]; } - this.withSocket(function(socket) { socket.send(Faye.toJSON(messages)) }); - }, - - withSocket: function(callback, context) { - this.callback(callback, context); + this.callback(function(socket) { socket.send(Faye.toJSON(messages)) }); this.connect(); }, @@ -1815,11 +1843,14 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, { this._state = this.CONNECTING; var ws = Faye.Transport.WebSocket.getClass(); - this._socket = new ws(Faye.Transport.WebSocket.getSocketUrl(this._endpoint)); + if (!ws) return this.setDeferredStatus('failed'); + + this._socket = new ws(Faye.Transport.WebSocket.getSocketUrl(this.endpoint)); var self = this; this._socket.onopen = function() { self._state = self.CONNECTED; + self._everConnected = true; self.setDeferredStatus('succeeded', self._socket); self.trigger('up'); }; @@ -1839,6 +1870,7 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, { delete self._socket; if (wasConnected) return self.resend(); + if (!self._everConnected) return self.setDeferredStatus('failed'); var retry = self._client.retry * 1000; Faye.ENV.setTimeout(function() { self.connect() }, retry); @@ -1847,12 +1879,11 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, { }, resend: function() { + if (!this._messages) return; var messages = Faye.map(this._messages, function(id, msg) { return msg }); this.request(messages); } }), { - WEBSOCKET_TIMEOUT: 1000, - getSocketUrl: function(endpoint) { if (Faye.URI) endpoint = Faye.URI.parse(endpoint).toURL(); return endpoint.replace(/^http(s?):/ig, 'ws$1:'); @@ -1864,30 +1895,14 @@ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, { Faye.ENV.MozWebSocket; }, - isUsable: function(endpoint, callback, context) { - var ws = this.getClass(); - if (!ws) return callback.call(context, false); - - var connected = false, - called = false, - socketUrl = this.getSocketUrl(endpoint), - socket = new ws(socketUrl); - - socket.onopen = function() { - connected = true; - socket.close(); - callback.call(context, true); - called = true; - socket = null; - }; - - var notconnected = function() { - if (!called && !connected) callback.call(context, false); - called = true; - }; - - socket.onclose = socket.onerror = notconnected; - Faye.ENV.setTimeout(notconnected, this.WEBSOCKET_TIMEOUT); + isUsable: function(client, endpoint, callback, context) { + this.create(client, endpoint).isUsable(callback, context); + }, + + create: function(client, endpoint) { + var sockets = client.transports.websocket = client.transports.websocket || {}; + sockets[endpoint] = sockets[endpoint] || new this(client, endpoint); + return sockets[endpoint]; } }); @@ -1903,26 +1918,41 @@ if (Faye.Event) Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, { initialize: function(client, endpoint) { Faye.Transport.prototype.initialize.call(this, client, endpoint); + if (!Faye.ENV.EventSource) return this.setDeferredStatus('failed'); + this._xhr = new Faye.Transport.XHR(client, endpoint); var socket = new EventSource(endpoint + '/' + client.getClientId()), self = this; socket.onopen = function() { + self._everConnected = true; + self.setDeferredStatus('succeeded'); self.trigger('up'); }; socket.onerror = function() { - self.trigger('down'); + if (self._everConnected) { + self.trigger('down'); + } else { + self.setDeferredStatus('failed'); + socket.close(); + } }; socket.onmessage = function(event) { self.receive(JSON.parse(event.data)); + self.trigger('up'); }; this._socket = socket; }, + isUsable: function(callback, context) { + this.callback(function() { callback.call(context, true) }); + this.errback(function() { callback.call(context, false) }); + }, + request: function(message, timeout) { this._xhr.request(message, timeout); }, @@ -1931,20 +1961,31 @@ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, { this._socket.close(); } }), { - isUsable: function(endpoint, callback, context) { - Faye.Transport.XHR.isUsable(endpoint, function(usable) { - callback.call(context, usable && Faye.ENV.EventSource); - }); + isUsable: function(client, endpoint, callback, context) { + var id = client.getClientId(); + if (!id) return callback.call(context, false); + + Faye.Transport.XHR.isUsable(client, endpoint, function(usable) { + if (!usable) return callback.call(context, false); + this.create(client, endpoint).isUsable(callback, context); + }, this); + }, + + create: function(client, endpoint) { + var sockets = client.transports.eventsource = client.transports.eventsource || {}; + sockets[endpoint] = sockets[endpoint] || new this(client, endpoint); + return sockets[endpoint]; } }); +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(message, timeout) { var retry = this.retry(message, timeout), - path = Faye.URI.parse(this._endpoint).pathname, + path = Faye.URI.parse(this.endpoint).pathname, self = this, xhr = Faye.ENV.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") @@ -1952,6 +1993,7 @@ Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, { xhr.open('POST', path, true); xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.setRequestHeader('Pragma', 'no-cache'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); var headers = this.headers; @@ -2002,8 +2044,8 @@ Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, { xhr.send(Faye.toJSON(message)); } }), { - isUsable: function(endpoint, callback, context) { - callback.call(context, Faye.URI.parse(endpoint).isLocal()); + isUsable: function(client, endpoint, callback, context) { + callback.call(context, Faye.URI.parse(endpoint).isSameOrigin()); } }); @@ -2016,7 +2058,8 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, { retry = this.retry(message, timeout), self = this; - xhr.open('POST', this._endpoint, true); + xhr.open('POST', this.endpoint, true); + if (xhr.setRequestHeader) xhr.setRequestHeader('Pragma', 'no-cache'); var cleanUp = function() { if (!xhr) return false; @@ -2056,8 +2099,8 @@ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, { xhr.send('message=' + encodeURIComponent(Faye.toJSON(message))); } }), { - isUsable: function(endpoint, callback, context) { - if (Faye.URI.parse(endpoint).isLocal()) + isUsable: function(client, endpoint, callback, context) { + if (Faye.URI.parse(endpoint).isSameOrigin()) return callback.call(context, false); if (Faye.ENV.XDomainRequest) @@ -2076,13 +2119,22 @@ Faye.Transport.register('cross-origin-long-polling', Faye.Transport.CORS); Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, { - request: function(message, timeout) { - var params = {message: Faye.toJSON(message)}, + shouldFlush: function(messages) { + var params = { + message: Faye.toJSON(messages), + jsonp: '__jsonp' + Faye.Transport.JSONP._cbCount + '__' + }; + var location = Faye.URI.parse(this.endpoint, params).toURL(); + return location.length >= Faye.Transport.MAX_URL_LENGTH; + }, + + request: function(messages, timeout) { + var params = {message: Faye.toJSON(messages)}, head = document.getElementsByTagName('head')[0], script = document.createElement('script'), callbackName = Faye.Transport.JSONP.getCallbackName(), - location = Faye.URI.parse(this._endpoint, params), - retry = this.retry(message, timeout), + location = Faye.URI.parse(this.endpoint, params), + retry = this.retry(messages, timeout), self = this; Faye.ENV[callbackName] = function(data) { @@ -2119,7 +2171,7 @@ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, { return '__jsonp' + this._cbCount + '__'; }, - isUsable: function(endpoint, callback, context) { + isUsable: function(client, endpoint, callback, context) { callback.call(context, true); } });