📄 jsjac.js
字号:
return null;};JSJaCConnection.prototype._parseStreamFeatures=function(doc){if(!doc){this.oDbg.log("nothing to parse ... aborting",1);return false;}this.mechs=new Object();var lMec1=doc.getElementsByTagName("mechanisms");this.has_sasl=false;for(var i=0;i<lMec1.length;i++)if(lMec1.item(i).getAttribute("xmlns")=="urn:ietf:params:xml:ns:xmpp-sasl"){this.has_sasl=true;var lMec2=lMec1.item(i).getElementsByTagName("mechanism");for(var j=0;j<lMec2.length;j++)this.mechs[lMec2.item(j).firstChild.nodeValue]=true;break;}this.has_sasl = false;if(this.has_sasl)this.oDbg.log("SASL detected",2);else{this.authtype='nonsasl';this.oDbg.log("No support for SASL detected",2);}};JSJaCConnection.prototype._process=function(timerval){if(!this.connected()){this.oDbg.log("Connection lost ...",1);if(this._interval)clearInterval(this._interval);return;}if(timerval)this.setPollInterval(timerval);if(this._timeout)clearTimeout(this._timeout);var slot=this._getFreeSlot();if(slot<0)return;if(typeof(this._req[slot])!='undefined'&&typeof(this._req[slot].r)!='undefined'&&this._req[slot].r.readyState!=4){this.oDbg.log("Slot "+slot+" is not ready");return;}if(!this.isPolling()&&this._pQueue.length==0&&this._req[(slot+1)%2]&&this._req[(slot+1)%2].r.readyState!=4){this.oDbg.log("all slots busy, standby ...",2);return;}if(!this.isPolling())this.oDbg.log("Found working slot at "+slot,2);this._req[slot]=this._setupRequest(true);this._req[slot].r.onreadystatechange=function(){if(typeof(oCon)=='undefined'||!oCon||!oCon.connected())return;oCon.oDbg.log("ready state changed for slot "+slot+" ["+oCon._req[slot].r.readyState+"]",4);if(oCon._req[slot].r.readyState==4){oCon._setStatus('processing');oCon.oDbg.log("async recv: "+oCon._req[slot].r.responseText,4);oCon._handleResponse(oCon._req[slot]);if(oCon._pQueue.length){oCon._timeout=setTimeout("oCon._process()",100);}else{oCon.oDbg.log("scheduling next poll in "+oCon.getPollInterval()+" msec",4);oCon._timeout=setTimeout("oCon._process()",oCon.getPollInterval());}}};if(typeof(this._req[slot].r.onerror)!='undefined'){this._req[slot].r.onerror=function(e){if(typeof(oCon)=='undefined'||!oCon||!oCon.connected())return;oCon._errcnt++;oCon.oDbg.log('XmlHttpRequest error ('+oCon._errcnt+')',1);if(oCon._errcnt>JSJAC_ERR_COUNT){oCon._abort();return false;}oCon._setStatus('onerror_fallback');setTimeout("oCon._resume()",oCon.getPollInterval());return false;};}var reqstr=this._getRequestString();if(typeof(this._rid)!='undefined')this._req[slot].rid=this._rid;this.oDbg.log("sending: "+reqstr,4);this._req[slot].r.send(reqstr);};JSJaCConnection.prototype._registerPID=function(pID,cb,arg){if(!pID||!cb)return false;this._regIDs[pID]=new Object();this._regIDs[pID].cb=cb;if(arg)this._regIDs[pID].arg=arg;this.oDbg.log("registered "+pID,3);return true;};JSJaCConnection.prototype._sendEmpty=function JSJaCSendEmpty(){var slot=this._getFreeSlot();this._req[slot]=this._setupRequest(true);this._req[slot].r.onreadystatechange=function(){if(typeof(oCon)=='undefined'||!oCon)return;if(oCon._req[slot].r.readyState==4){oCon.oDbg.log("async recv: "+oCon._req[slot].r.responseText,4);oCon._getStreamID(slot);}}if(typeof(this._req[slot].r.onerror)!='undefined'){this._req[slot].r.onerror=function(e){if(typeof(oCon)=='undefined'||!oCon||!oCon.connected())return;oCon.oDbg.log('XmlHttpRequest error',1);return false;};}var reqstr=this._getRequestString();this.oDbg.log("sending: "+reqstr,4);this._req[slot].r.send(reqstr);};JSJaCConnection.prototype._setStatus=function(status){if(!status||status=='')return;if(status!=this._status){this._status=status;this._handleEvent('onstatuschanged',status);this._handleEvent('status_changed',status);}};JSJaCConnection.prototype._unregisterPID=function(pID){if(!this._regIDs[pID])return false;this._regIDs[pID]=null;this.oDbg.log("unregistered "+pID,3);return true;};function JSJaCConsoleLogger(level){this.level=level||4;this.start=function(){};this.log=function(msg,level){level=level||0;if(level>this.level)return;if(typeof(console)=='undefined')return;try{switch(level){case 0:console.warn(msg);break;case 1:console.error(msg);break;case 2:console.info(msg);break;case 4:console.debug(msg);break;default:console.log(msg);break;}}catch(e){try{console.log(msg)}catch(e){}}};this.setLevel=function(level){this.level=level;return this;}this.getLevel=function(){return this.level;}}function JSJaCCookie(name,value,secs){if(window==this)return new JSJaCCookie(name,value,secs);this.name=name;this.value=value;this.expires=secs;this.write=function(){if(this.secs){var date=new Date();date.setTime(date.getTime()+(this.secs*1000));var expires="; expires="+date.toGMTString();}elsevar expires="";document.cookie=this.getName()+"="+this.getValue()+expires+"; path=/";};this.erase=function(){var c=new JSJaCCookie(this.getName(),"",-1);c.write();}this.getName=function(){return this.name;}this.setName=function(name){this.name=name;return this;}this.getValue=function(){return this.value;}this.setValue=function(value){this.value=value;return this;}}JSJaCCookie.read=function(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return new JSJaCCookie(name,c.substring(nameEQ.length,c.length));}throw new JSJaCCookieException("Cookie not found");};JSJaCCookie.get=function(name){return JSJaCCookie.read(name).getValue();};JSJaCCookie.remove=function(name){JSJaCCookie.read(name).erase();};function JSJaCCookieException(msg){this.message=msg;this.name="CookieException";}function JSJaCError(code,type,condition){var xmldoc=XmlDocument.create("error","jsjac");xmldoc.documentElement.setAttribute('code',code);xmldoc.documentElement.setAttribute('type',type);xmldoc.documentElement.appendChild(xmldoc.createElement(condition)).setAttribute('xmlns','urn:ietf:params:xml:ns:xmpp-stanzas');return xmldoc.documentElement;}function JSJaCHttpBindingConnection(oArg){this.base=JSJaCConnection;this.base(oArg);this._hold=JSJACHBC_MAX_HOLD;this._inactivity=0;this._last_requests=new Object();this._last_rid=0;this._min_polling=0;this._pause=0;this._wait=JSJACHBC_MAX_WAIT;this.connect=JSJaCHBCConnect;this.disconnect=JSJaCHBCDisconnect;this.inherit=JSJaCHBCInherit;this.isPolling=function(){return(this._hold==0)};this.setPollInterval=function(timerval){if(!timerval||isNaN(timerval)){this.oDbg.log("Invalid timerval: "+timerval,1);return-1;}if(!this.isPolling())this._timerval=100;else if(this._min_polling&&timerval<this._min_polling*1000)this._timerval=this._min_polling*1000;else if(this._inactivity&&timerval>this._inactivity*1000)this._timerval=this._inactivity*1000;elsethis._timerval=timerval;return this._timerval;};this._getRequestString=JSJaCHBCGetRequestString;this._getFreeSlot=function(){for(var i=0;i<this._hold+1;i++)if(typeof(this._req[i])=='undefined'||typeof(this._req[i].r)=='undefined'||this._req[i].r.readyState==4)return i;return-1;}this._getHold=function(){return this._hold;}this._getStreamID=JSJaCHBCGetStreamID;this._getSuspendVars=function(){return('host,port,secure,_rid,_last_rid,_wait,_min_polling,_inactivity,_hold,_last_requests,_pause').split(',');}this._handleInitialResponse=JSJaCHBCHandleInitialResponse;this._prepareResponse=JSJaCHBCPrepareResponse;this._reInitStream=JSJaCHBCReInitStream;this._resume=function(){if(this._pause==0&&this._rid>=this._last_rid)this._rid=this._last_rid-1;this._process();this._inQto=setInterval("oCon._checkInQ();",JSJAC_CHECKINQUEUEINTERVAL);this._interval=setInterval("oCon._checkQueue()",JSJAC_CHECKQUEUEINTERVAL);}this._setHold=function(hold){if(!hold||isNaN(hold)||hold<0)hold=0;else if(hold>JSJACHBC_MAX_HOLD)hold=JSJACHBC_MAX_HOLD;this._hold=hold;return this._hold;};this._setupRequest=JSJaCHBCSetupRequest;this._suspend=function(){if(this._pause==0)return;var slot=this._getFreeSlot();this._req[slot]=this._setupRequest(false);var reqstr="<body pause='"+this._pause+"' xmlns='http://jabber.org/protocol/httpbind' sid='"+this._sid+"' rid='"+this._rid+"'";if(JSJAC_HAVEKEYS){reqstr+=" key='"+this._keys.getKey()+"'";if(this._keys.lastKey()){this._keys=new JSJaCKeys(hex_sha1,this.oDbg);reqstr+=" newkey='"+this._keys.getKey()+"'";}}reqstr+=">";while(this._pQueue.length){var curNode=this._pQueue[0];reqstr+=curNode;this._pQueue=this._pQueue.slice(1,this._pQueue.length);}reqstr+="</body>";var abortTimerID=setTimeout("oCon._req["+slot+"].r.abort();",5000);this.oDbg.log("Disconnecting: "+reqstr,4);this._req[slot].r.send(reqstr);clearTimeout(abortTimerID);}}JSJaCHttpBindingConnection.prototype=new JSJaCConnection();function JSJaCHBCConnect(oArg){this._setStatus('connecting');this.domain=oArg.domain||'localhost';this.username=oArg.username;this.resource=oArg.resource;this.pass=oArg.pass;this.register=oArg.register;this.oDbg.log("httpbase: "+this._httpbase+"\domain:"+this.domain,2);this.host=oArg.host||this.domain;this.port=oArg.port||5222;this.authhost=oArg.authhost||this.domain;this.authtype=oArg.authtype||'sasl';if(oArg.secure){this.secure='true';}elsethis.secure='false';this.jid=this.username+'@'+this.domain;this.fulljid=this.jid+'/'+this.resource;if(oArg.wait)this._wait=oArg.wait;if(oArg.xmllang&&oArg.xmllang!='')this._xmllang=oArg.xmllang;this._rid=Math.round(100000.5+(((900000.49999)-(100000.5))*Math.random()));var slot=this._getFreeSlot();this._req[slot]=this._setupRequest(true);var reqstr="<body hold='"+this._hold+"' xmlns='http://jabber.org/protocol/httpbind' to='"+this.authhost+"' wait='"+this._wait+"' rid='"+this._rid+"'";if(oArg.host||oArg.port)reqstr+=" route='xmpp:"+this.host+":"+this.port+"'";if(oArg.secure)reqstr+=" secure='"+this.secure+"'";if(JSJAC_HAVEKEYS){this._keys=new JSJaCKeys(hex_sha1,this.oDbg);key=this._keys.getKey();reqstr+=" newkey='"+key+"'";}if(this._xmllang)reqstr+=" xml:lang='"+this._xmllang+"'";if(JSJACHBC_USE_BOSH_VER){reqstr+=" ver='"+JSJACHBC_BOSH_VERSION+"'";reqstr+=" xmpp:xmlns='urn:xmpp:xbosh'";reqstr+=" xmpp:version='1.0'";}reqstr+="/>";this.oDbg.log(reqstr,4);this._req[slot].r.onreadystatechange=function(){if(typeof(oCon)=='undefined'||!oCon)return;if(oCon._req[slot].r.readyState==4){oCon.oDbg.log("async recv: "+oCon._req[slot].r.responseText,4);oCon._handleInitialResponse(slot);}}if(typeof(this._req[slot].r.onerror)!='undefined'){this._req[slot].r.onerror=function(e){if(typeof(oCon)=='undefined'||!oCon||!oCon.connected())return;oCon.oDbg.log('XmlHttpRequest error',1);return false;};}this._req[slot].r.send(reqstr);}function JSJaCHBCHandleInitialResponse(slot){try{this.oDbg.log(this._req[slot].r.getAllResponseHeaders(),4);this.oDbg.log(this._req[slot].r.responseText,4);}catch(ex){this.oDbg.log("No response",4);}if(this._req[slot].r.status!=200||!this._req[slot].r.responseXML){this.oDbg.log("initial response broken (status: "+this._req[slot].r.status+")",1);this._handleEvent('onerror',JSJaCError('503','cancel','service-unavailable'));return;}var body=this._req[slot].r.responseXML.documentElement;if(!body||body.tagName!='body'||body.namespaceURI!='http://jabber.org/protocol/httpbind'){this.oDbg.log("no body element or incorrect body in initial response",1);this._handleEvent("onerror",JSJaCError("500","wait","internal-service-error"));return;}if(body.getAttribute("type")=="terminate"){this.oDbg.log("invalid response:\n"+this._req[slot].r.responseText,1);clearTimeout(this._timeout);this._connected=false;this.oDbg.log("Disconnected.",1);this._handleEvent('ondisconnect');this._handleEvent('onerror',JSJaCError('503','cancel','service-unavailable'));return;}this._sid=body.getAttribute('sid');this.oDbg.log("got sid: "+this._sid,2);if(body.getAttribute('polling'))this._min_polling=body.getAttribute('polling');if(body.getAttribute('inactivity'))this._inactivity=body.getAttribute('inactivity');if(body.getAttribute('requests'))this._setHold(body.getAttribute('requests')-1);this.oDbg.log("set hold to "+this._getHold(),2);if(body.getAttribute('ver'))this._bosh_version=body.getAttribute('ver');if(body.getAttribute('maxpause'))this._pause=Number.max(body.getAttribute('maxpause'),JSJACHBC_MAXPAUSE);this.setPollInterval(this._timerval);this._connected=true;this._inQto=setInterval("oCon._checkInQ();",JSJAC_CHECKINQUEUEINTERVAL);this._interval=setInterval("oCon._checkQueue()",JSJAC_CHECKQUEUEINTERVAL);this._getStreamID(slot);}function JSJaCHBCGetStreamID(slot){this.oDbg.log(this._req[slot].r.responseText,4);if(!this._req[slot].r.responseXML||!this._req[slot].r.responseXML.documentElement){this._handleEvent('onerror',JSJaCError('503','cancel','service-unavailable'));return;}var body=this._req[slot].r.responseXML.documentElement;if(body.getAttribute('authid')){this.streamid=body.getAttribute('authid');this.oDbg.log("got streamid: "+this.streamid,2);}else{this._timeout=setTimeout("oCon._sendEmpty()",this.getPollInterval());return;}this._timeout=setTimeout("oCon._process()",this.getPollInterval());this._parseStreamFeatures(body);if(this.register)this._doInBandReg();elsethis._doAuth();}function JSJaCHBCInherit(oArg){this.domain=oArg.domain||'localhost';this.username=oArg.username;this.resource=oArg.resource;this._sid=oArg.sid;this._rid=oArg.rid;this._min_polling=oArg.polling;this._inactivity=oArg.inactivity;this._setHold(oArg.requests-1);this.setPollInterval(this._timerval);if(oArg.wait)this._wait=oArg.wait;this._connected=true;this._handleEvent('onconnect');this._interval=setInterval("oCon._checkQueue()",JSJAC_CHECKQUEUEINTERVAL);this._inQto=setInterval("oCon._checkInQ();",JSJAC_CHECKINQUEUEINTERVAL);this._timeout=setTimeout("oCon._process()",this.getPollInterval());}function JSJaCHBCReInitStream(to,cb,arg){oCon._reinit=true;eval("oCon."+cb+"("+arg+")");}function JSJaCHBCDisconnect(){this._setStatus('disconnecting');if(!this.connected())return;this._connected=false;clearInterval(this._interval);clearInterval(this._inQto);if(this._timeout)clearTimeout(this._timeout);var slot=this._getFreeSlot();this._req[slot]=this._setupRequest(false);var reqstr="<body type='terminate' xmlns='http://jabber.org/protocol/httpbind' sid='"+this._sid+"' rid='"+this._rid+"'";if(JSJAC_HAVEKEYS){reqstr+=" key='"+this._keys.getKey()+"'";}reqstr+=">";while(this._pQueue.length){var curNode=this._pQueue[0];reqstr+=curNode;this._pQueue=this._pQueue.slice(1,this._pQueue.length);}reqstr+="</body>";var abortTimerID=setTimeout("oCon._req["+slot+"].r.abort();",5000);this.oDbg.log("Disconnecting: "+reqstr,4);this._req[slot].r.send(reqstr);clearTimeout(abortTimerID);try{JSJaCCookie.read('JSJaC_State').erase();}catch(e){}oCon.oDbg.log("Disconnected: "+oCon._req[slot].r.responseText,2);oCon._handleEvent('ondisconnect');}function JSJaCHBCSetupRequest(async){var req=new Object();var r=XmlHttp.create();try{r.open("POST",this._httpbase,async);r.setRequestHeader('Content-Type','text/xml; charset=utf-8');}catch(e){this.oDbg.log(e,1);}req.r=r;this._rid++;req.rid=this._rid;return req;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -