Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Trap.Transports.HTTP=function(){Trap.AbstractTransport.call(this);this._transportPriority=100;this.expirationDelay=28000;this.connectionTimeout=10000;this.latencyEstimate=1000;this._buf=[]};Trap.Transports.HTTP.prototype=new Trap.AbstractTransport;Trap.Transports.HTTP.prototype.constructor=new Trap.Transports.HTTP;Trap.Transports.HTTP.CONFIG_URL="url";try{Trap.Transports.HTTP.prototype.supportsBinary=typeof new XMLHttpRequest().responseType==="string"}catch(e){Trap.Transports.HTTP.prototype.supportsBinary=false}Trap.Transports.HTTP.prototype.init=function(){this._buf=[];if(this._longpoll){this._longpoll.onreadystatechange=function(){}}Trap.AbstractTransport.prototype.init.call(this)};Trap.Transports.HTTP.prototype.getTransportName=function(){return"http"};Trap.Transports.HTTP.prototype.getProtocolName=function(){return"http"};Trap.Transports.HTTP.prototype.updateConfig=function(){Trap.AbstractTransport.prototype.updateConfig.call(this);if((this.getState()==Trap.Transport.State.DISCONNECTED)||(this.getState()==Trap.Transport.State.CONNECTING)){this.url=this.getOption(Trap.Transports.HTTP.CONFIG_URL)}else{this.logger.debug("Updating HTTP configuration while open; changes will not take effect until HTTP is reconnected")}this.expirationDelay=this._configuration.getIntOption(this._prefix+".expirationDelay",this.expirationDelay);this.connectionTimeout=this._configuration.getIntOption(this._prefix+".connectionTimeout",this.connectionTimeout)};Trap.Transports.HTTP.prototype.fillAuthenticationKeys=function(b){};Trap.Transports.HTTP.prototype.updateContext=function(){};Trap.Transports.HTTP.prototype.internalSend=function(m,k){var o=this;o._sendQueued=true;if(!!m){this._buf.push(m)}if(k){if(o._sendTimer){clearTimeout(o._sendTimer)}o._sendTimer=setTimeout(function(){o.internalSend(null,false)},1000);return}if(o._sendTimer){clearTimeout(o._sendTimer);o._sendTimer=null}if(this._buf.length==0){return}var i=(this.useBinary?new Trap.ByteArrayOutputStream():new Trap.ByteStringOutputStream());for(var n=0;n<this._buf.length;n++){i.write(this._buf[n].serialize(this.useBinary))}var l=(this.useBinary?i.toArray():i.toString());if(o.getState()==Trap.Transport.State.AVAILABLE||o.getState()==Trap.Transport.State.CONNECTED){o.setState(Trap.Transport.State.UNAVAILABLE)}var j=this.openConnection("POST");j.setRequestHeader("Content-Type","x-trap");j.send(l.buffer?l.buffer:l);j.onreadystatechange=function(){if(j.readyState==4){if(j.hasError||j.hasTimeout||j.isAborted){o._dispatchEvent({type:"failedsending",messages:o._buf});o.setState(Trap.Transport.State.ERROR)}else{for(var a=0;a<o._buf.length;a++){if(o._buf[a].getMessageId()>0){o._dispatchEvent({type:"messagesent",data:o._buf[a],message:o._buf[a]})}}o._buf=[];if(o.getState()==Trap.Transport.State.UNAVAILABLE||o.getState()==Trap.Transport.State.CONNECTED){o.setState(Trap.Transport.State.AVAILABLE)}}o._sendQueued=false}}};Trap.Transports.HTTP.prototype.isClientConfigured=function(){return this.url&&typeof(this.url)=="string"&&this.url.length>4};Trap.Transports.HTTP.prototype.internalConnect=function(){this.logger.debug("HTTP Transport Opening...");if(!this.isClientConfigured()){this.logger.debug("HTTP Transport not properly configured... Unless autoconfigure is enabled (and another transport succeeds) this transport will not be available.");this.setState(Trap.Transport.State.ERROR);return}var d=this;try{var f=this.openConnection("GET");f.responseType="text";f.onreadystatechange=function(){if(f.readyState==4){if(f.status==200&&!f.hasError&&!f.hasTimeout&&!f.isAborted){if("/"==d.url.charAt(d.url.length-1)){d.url=d.url+f.responseText}else{d.url=d.url+"/"+f.responseText}d.running=true;d.poll();d.setState(Trap.Transport.State.CONNECTED)}else{d.logger.warn("HTTP transport failed with state ",f.status);d.setState(Trap.Transport.State.ERROR);return true}}return false};f.send()}catch(g){this.logger.warn("HTTP transport failed to connect due to ",g);if(g.stack){this.logger.debug(g.stack)}this.setState(Trap.Transport.State.ERROR);return}};Trap.Transports.HTTP.prototype.internalDisconnect=function(){var g=this;if(g._sendQueued){setTimeout(function(){g.internalDisconnect()},100);return}var f=new XMLHttpRequest();f.open("POST",this.url,true);var d=function(){g.running=false;if(g.getState()==Trap.Transport.State.DISCONNECTING){g.setState(Trap.Transport.State.DISCONNECTED)}};f.onreadystatechange=function(){if(f.readyState==4){d()}};f.send()};Trap.Transports.HTTP.prototype.canConnect=function(){return true};Trap.Transports.HTTP.prototype.poll=function(){if(!this.running){return}var f=this.openConnection("GET");if(this.useBinary){try{f.responseType="arraybuffer"}catch(g){console.error("Asked to use binary but could not use binary mode transport!");this.setState(Trap.Transport.State.ERROR);return}}var d=this;f.onreadystatechange=function(){if(f.readyState==4){if(f.isAborted){if(f.abortState==2){d.poll();d._keepalivePredictor.dataReceived()}else{d.setState(Trap.Transport.State.ERROR)}}else{if(f.status<300&&f.status>=200){if(f.responseType=="arraybuffer"){try{if(f.response){var b=new Uint8Array(f.response);d.receive(b)}if(f.status!=0||f.statusText.length!=0){d.poll()}}catch(a){console.log(a)}}else{var b=f.responseText;b=b.toUTF8ByteArray();d.receive(b,0,b.length);d.poll();d._keepalivePredictor.dataReceived()}}else{if(f.status==0||f.status>=300){if(d.getState()!=Trap.Transport.State.DISCONNECTING&&d.getState()!=Trap.Transport.State.DISCONNECTED){d.setState(Trap.Transport.State.ERROR)}}}}}};f.send();d._keepalivePredictor.dataSent();this._longpoll=f};Trap.Transports.HTTP.prototype.openConnection=function(m){var l=new XMLHttpRequest();l.open(m,this.url+"?expires="+this.expirationDelay,true);l.aborted=false;l.responseType==="arraybuffer";var u=this;function q(a){l.isAborted=true;l.abortState=l.readyState;l.abort()}var t=null;var p=function(){if(l.readyState==1){q();u.logger.warn("XHR longpoll failed to connect...")}};l.connectionTimer=setTimeout(p,this.expirationDelay+this.latencyEstimate*3);var n=false;var s=new Date().valueOf();function o(){if(n){return}n=true;var b=new Date().valueOf();var a=b-s;u.latencyEstimate=(u.latencyEstimate+a)/2}if(l.upload){l.upload.addEventListener("loadstart",function(){clearTimeout(l.connectionTimer);n=true;var c=false;var d=1000;var b=false;l.upload.addEventListener("progress",function(){c=true},true);l.upload.addEventListener("error",function(){l.hasError=true},true);l.upload.addEventListener("timeout",function(){l.hasTimeout=true},true);l.upload.addEventListener("load",function(){clearTimeout(f);b=true;l.connectionTimer=setTimeout(p,u.connectionTimeout)},true);l.upload.addEventListener("loadend",function(){if(!b){u.logger.warn("Incomplete upload: loadend without load");l.hasError=true}},true);var a=function(){if(!u.running){return}if(l.readyState==4){return}if(!c){q();return}c=false;setTimeout(a,d)};var f=setTimeout(a,u.connectionTimeout)},true)}l.addEventListener("loadstart",function(){u.logger.trace("XHR load started...")});l.addEventListener("readystatechange",function(){switch(l.readyState){case 0:break;case 1:break;case 2:u.logger.debug("XHR switched state to headers received (we have connection to server). Stopping connectionTimeout, starting pollTimeout");clearTimeout(l.connectionTimer);o();t=setTimeout(function(){if(!u.running){return}switch(l.readyState){case 0:case 1:q();u.logger.warn("XHR ended in an inconsistent state...");u.setState(Trap.Transport.State.ERROR);return;case 2:q();u.logger.debug("Loading failed after headers loaded");return;case 3:break;case 4:u.logger.error("HTTP transport in inconsistent state");u.setState(Trap.Transport.State.ERROR);return}var a=false;l.onprogress=function(){a=true};var b=function(){if(!u.running){return}if(l.readyState==4){return}if(!a){q();return}a=false;setTimeout(b,100)};setTimeout(b,100)},30000);break;case 3:u.logger.debug("XHR switched state to Receiving (data incoming from server)");break;case 4:clearTimeout(t);if(l.hasError||l.hasTimeout){u.setState(Trap.Transport.State.ERROR)}break}},true);var r=false;l.addEventListener("error",function(){l.hasError=true},true);l.addEventListener("timeout",function(){l.hasTimeout=true},true);l.addEventListener("load",function(){r=true},true);l.addEventListener("loadend",function(){if(!r){u.logger.warn("Incomplete download: loadend without load");l.hasError=true}},true);return l};Trap.Transports.HTTP.prototype.flushTransport=function(){this.internalSend(null,false)};Trap.Transports.HTTP.prototype.setState=function(){if(this.getState()==Trap.Transport.State.DISCONNECTED||this.getState()==Trap.Transport.State.ERROR){this.running=false}Trap.AbstractTransport.prototype.setState.apply(this,arguments)};
New to GrepCode? Check out our FAQ X