📄 cometda.js
字号:
}
if(deferred){
deferred.callback(true);
}
this.subscribed(message.subscription, message);
} catch(e) {
log.warn(e);
}
break;
case "/meta/unsubscribe":
deferred = this._deferredUnsubscribes[message.subscription];
try
{
if(!message.successful){
if(deferred){
deferred.errback(new Error(message.error));
}
this.currentTransport.cancelConnect();
return;
}
if(deferred){
deferred.callback(true);
}
this.unsubscribed(message.subscription, message);
} catch(e) {
log.warn(e);
}
break;
default:
if(message.successful && !message.successful){
this.currentTransport.cancelConnect();
return;
}
}
}
this.currentTransport.deliver(message);
if(message.data){
try{
var messages = [message];
var tname = prefix + message.channel;
var tnameParts = message.channel.split("/");
var tnameGlob = prefix;
for (var i = 1; i < tnameParts.length - 1; i++){
dojo.publish(tnameGlob + "/**", messages);
tnameGlob += "/" + tnameParts[i];
}
dojo.publish(tnameGlob + "/**", messages);
dojo.publish(tnameGlob + "/*", messages);
dojo.publish(tname,messages);
}catch(e){
}
}
};
this._sendMessage = function(/* object */ message){
if(this.currentTransport && !this.batch){
return this.currentTransport.sendMessages([message]);
}else{
this._messageQ.push(message);
return null;
}
};
this.startBatch = function(){
this.batch++;
};
this.endBatch = function(){
if(--this.batch <= 0 && this.currentTransport && this._connected){
this.batch = 0;
var messages = this._messageQ;
this._messageQ = [];
if(messages.length > 0){
this.currentTransport.sendMessages(messages);
}
}
};
this._onUnload = function(){
dojo.addOnUnload(dojox.cometd, "disconnect");
};
this._connectTimeout = function(){
var advised=0;
if(this._advice && this._advice.timeout && this.expectedNetworkDelay > 0){
advised = this._advice.timeout + this.expectedNetworkDelay;
}
if(this.connectTimeout > 0 && this.connectTimeout < advised){
return this.connectTimeout;
}
return advised;
};
},
connectionTypes : new dojo.AdapterRegistry(true)
};
dojox.cometd.Connection.call(dojox.cometd,"/cometd");
dojo.addOnUnload(dojox.cometd, "_onUnload");
//longPollTransport.js longPollTransportJsonEncoded.js
dojox.cometd.longPollTransportJsonEncoded = new function(){
this._connectionType="long-polling";
this._cometd=null;
this.check = function(types, version, xdomain){
return ((!xdomain)&&(dojo.indexOf(types, "long-polling") >= 0));
};
this.tunnelInit = function(){
var message = {
channel: "/meta/connect",
clientId: this._cometd.clientId,
connectionType: this._connectionType,
id: ""+this._cometd.messageId++
};
message=this._cometd._extendOut(message);
this.openTunnelWith([message]);
};
this.tunnelCollapse = function(){
if(!this._cometd._initialized){ return; }
if(this._cometd._advice && this._cometd._advice["reconnect"]=="none"){
return;
}
if (this._cometd._connected) {
setTimeout(dojo.hitch(this,function(){this._connect();}),this._cometd._interval());
}else{
setTimeout(dojo.hitch(this._cometd,function(){this.init(this.url,this._props);}),this._cometd._interval());
}
};
this._connect = function(){
if(!this._cometd._initialized){ return; }
if(this._cometd._polling) {
return;
}
if((this._cometd._advice) && (this._cometd._advice["reconnect"]=="handshake")){
this._cometd._connected=false;
this._initialized = false;
this._cometd.init(this._cometd.url,this._cometd._props);
}else if(this._cometd._connected){
var message={
channel: "/meta/connect",
connectionType: this._connectionType,
clientId: this._cometd.clientId,
id: ""+this._cometd.messageId++
};
if (this._cometd.connectTimeout>=this._cometd.expectedNetworkDelay){
message.advice={timeout:(this._cometd.connectTimeout-this._cometd.expectedNetworkDelay)};
}
message=this._cometd._extendOut(message);
this.openTunnelWith([message]);
}
};
this.deliver = function(message){
// Nothing to do
};
this.openTunnelWith = function(messages, url){
this._cometd._polling = true;
var post = {
url: (url||this._cometd.url),
postData: dojo.toJson(messages),
contentType: "text/json;charset=UTF-8",
handleAs: this._cometd.handleAs,
load: dojo.hitch(this, function(data){
this._cometd._polling=false;
this._cometd.deliver(data);
this._cometd._backon();
this.tunnelCollapse();
}),
error: dojo.hitch(this, function(err){
this._cometd._polling=false;
this._cometd._publishMeta("connect",false);
this._cometd._backoff();
this.tunnelCollapse();
})
};
var connectTimeout=this._cometd._connectTimeout();
if (connectTimeout>0) {
post.timeout=connectTimeout;
}
this._poll = dojo.rawXhrPost(post);
};
this.sendMessages = function(messages){
for(var i=0; i<messages.length; i++){
messages[i].clientId = this._cometd.clientId;
messages[i].id = ""+this._cometd.messageId++;
messages[i]=this._cometd._extendOut(messages[i]);
}
return dojo.rawXhrPost({
url: this._cometd.url||dojo.config["cometdRoot"],
handleAs: this._cometd.handleAs,
load: dojo.hitch(this._cometd, "deliver"),
postData: dojo.toJson(messages),
contentType: "text/json;charset=UTF-8",
error: dojo.hitch(this, function(err){
this._cometd._publishMeta("publish",false,{messages:messages});
}),
timeout: this._cometd.expectedNetworkDelay
});
};
this.startup = function(handshakeData){
if(this._cometd._connected){ return; }
this.tunnelInit();
};
this.disconnect = function(){
var message = {
channel: "/meta/disconnect",
clientId: this._cometd.clientId,
id: "" + this._cometd.messageId++
};
message = this._cometd._extendOut(message);
dojo.rawXhrPost({
url: this._cometd.url || dojo.config["cometdRoot"],
handleAs: this._cometd.handleAs,
postData: dojo.toJson([message]),
contentType: "text/json;charset=UTF-8"
});
};
this.cancelConnect = function(){
if(this._poll){
this._poll.cancel();
this._cometd._polling=false;
this._cometd._publishMeta("connect",false,{cancel:true});
this._cometd._backoff();
this.disconnect();
this.tunnelCollapse();
}
};
};
dojox.cometd.longPollTransport = dojox.cometd.longPollTransportJsonEncoded;
dojox.cometd.connectionTypes.register("long-polling", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded);
dojox.cometd.connectionTypes.register("long-polling-json-encoded", dojox.cometd.longPollTransport.check, dojox.cometd.longPollTransportJsonEncoded);
//script.js
dojo.io = {};
dojo.io.script = {
get: function(/*dojo.io.script.__ioArgs*/args){
var dfd = this._makeScriptDeferred(args);
var ioArgs = dfd.ioArgs;
dojo._ioAddQueryToUrl(ioArgs);
if(this._canAttach(ioArgs)){
this.attach(ioArgs.id, ioArgs.url, args.frameDoc);
}
dojo._ioWatch(dfd, this._validCheck, this._ioCheck, this._resHandle);
return dfd;
},
attach: function(/*String*/id, /*String*/url, /*Document?*/frameDocument){
var doc = (frameDocument || dojo.doc);
var element = doc.createElement("script");
element.type = "text/javascript";
element.src = url;
element.id = id;
element.charset = "utf-8";
doc.getElementsByTagName("head")[0].appendChild(element);
},
remove: function(/*String*/id, /*Document?*/frameDocument){
dojo._destroyElement(dojo.byId(id, frameDocument));
if(this["jsonp_" + id]){
delete this["jsonp_" + id];
}
},
_makeScriptDeferred: function(/*Object*/args){
var dfd = dojo._ioSetArgs(args, this._deferredCancel, this._deferredOk, this._deferredError);
var ioArgs = dfd.ioArgs;
ioArgs.id = dojo._scopeName + "IoScript" + (this._counter++);
ioArgs.canDelete = false;
if(args.callbackParamName){
ioArgs.query = ioArgs.query || "";
if(ioArgs.query.length > 0){
ioArgs.query += "&";
}
ioArgs.query += args.callbackParamName
+ "="
+ (args.frameDoc ? "parent." : "")
+ dojo._scopeName + ".io.script.jsonp_" + ioArgs.id + "._jsonpCallback";
ioArgs.frameDoc = args.frameDoc;
ioArgs.canDelete = true;
dfd._jsonpCallback = this._jsonpCallback;
this["jsonp_" + ioArgs.id] = dfd;
}
return dfd;
},
_deferredCancel: function(/*Deferred*/dfd){
dfd.canceled = true;
if(dfd.ioArgs.canDelete){
dojo.io.script._addDeadScript(dfd.ioArgs);
}
},
_deferredOk: function(/*Deferred*/dfd){
if(dfd.ioArgs.canDelete){
dojo.io.script._addDeadScript(dfd.ioArgs);
}
if(dfd.ioArgs.json){
return dfd.ioArgs.json;
}else{
return dfd.ioArgs;
}
},
_deferredError: function(/*Error*/error, /*Deferred*/dfd){
if(dfd.ioArgs.canDelete){
if(error.dojoType == "timeout"){
dojo.io.script.remove(dfd.ioArgs.id, dfd.ioArgs.frameDoc);
}else{
dojo.io.script._addDeadScript(dfd.ioArgs);
}
}
return error;
},
_deadScripts: [],
_counter: 1,
_addDeadScript: function(/*Object*/ioArgs){
dojo.io.script._deadScripts.push({id: ioArgs.id, frameDoc: ioArgs.frameDoc});
ioArgs.frameDoc = null;
},
_validCheck: function(/*Deferred*/dfd){
var _self = dojo.io.script;
var deadScripts = _self._deadScripts;
if(deadScripts && deadScripts.length > 0){
for(var i = 0; i < deadScripts.length; i++){
//Remove the script tag
_self.remove(deadScripts[i].id, deadScripts[i].frameDoc);
deadScripts[i].frameDoc = null;
}
dojo.io.script._deadScripts = [];
}
return true;
},
_ioCheck: function(/*Deferred*/dfd){
if(dfd.ioArgs.json){
return true;
}
var checkString = dfd.ioArgs.args.checkString;
if(checkString && eval("typeof(" + checkString + ") != 'undefined'")){
return true;
}
return false;
},
_resHandle: function(/*Deferred*/dfd){
if(dojo.io.script._ioCheck(dfd)){
dfd.callback(dfd);
}else{
dfd.errback(new Error("inconceivable dojo.io.script._resHandle error"));
}
},
_canAttach: function(/*Object*/ioArgs){
return true;
},
_jsonpCallback: function(/*JSON Object*/json){
this.ioArgs.json = json;
}
};
//callbackPollTransport.js
dojox.cometd.callbackPollTransport = new function(){
this._connectionType = "callback-polling";
this._cometd = null;
this.check = function(types, version, xdomain){
return (dojo.indexOf(types, "callback-polling") >= 0);
};
this.tunnelInit = function(){
var message = {
channel: "/meta/connect",
clientId: this._cometd.clientId,
connectionType: this._connectionType,
id: "" + this._cometd.messageId++
};
message = this._cometd._extendOut(message);
this.openTunnelWith([message]);
};
this.tunnelCollapse = dojox.cometd.longPollTransport.tunnelCollapse;
this._connect = dojox.cometd.longPollTransport._connect;
this.deliver = dojox.cometd.longPollTransport.deliver;
this.openTunnelWith = function(content, url){
this._cometd._polling = true;
var script = {
load: dojo.hitch(this, function(data){
this._cometd._polling=false;
this._cometd.deliver(data);
this._cometd._backon();
this.tunnelCollapse();
}),
error: dojo.hitch(this, function(err){
this._cometd._polling = false;
this._cometd._publishMeta("connect",false);
this._cometd._backoff();
this.tunnelCollapse();
}),
url: (url || this._cometd.url),
content: { message: dojo.toJson(content) },
callbackParamName: "jsonp"
};
var connectTimeout = this._cometd._connectTimeout();
if(connectTimeout > 0){
script.timeout=connectTimeout;
}
dojo.io.script.get(script);
};
this.sendMessages = function(/*array*/ messages){
for(var i = 0; i < messages.length; i++){
messages[i].clientId = this._cometd.clientId;
messages[i].id = ""+this._cometd.messageId++;
messages[i]=this._cometd._extendOut(messages[i]);
}
var bindArgs = {
url: this._cometd.url || dojo.config["cometdRoot"],
load: dojo.hitch(this._cometd, "deliver"),
callbackParamName: "jsonp",
content: { message: dojo.toJson( messages ) },
error: dojo.hitch(this, function(err){
this._cometd._publishMeta("publish",false,{messages:messages});
}),
timeout: this._cometd.expectedNetworkDelay
};
return dojo.io.script.get(bindArgs);
};
this.startup = function(handshakeData){
if(this._cometd._connected){ return; }
this.tunnelInit();
};
this.disconnect = dojox.cometd.longPollTransport.disconnect;
this.disconnect = function(){
var message = {
channel: "/meta/disconnect",
clientId: this._cometd.clientId,
id: "" + this._cometd.messageId++
};
message = this._cometd._extendOut(message);
dojo.io.script.get({
url: this._cometd.url || dojo.config["cometdRoot"],
callbackParamName: "jsonp",
content: { message: dojo.toJson([message]) }
});
};
this.cancelConnect = function(){}
};
dojox.cometd.connectionTypes.register("callback-polling", dojox.cometd.callbackPollTransport.check, dojox.cometd.callbackPollTransport);
//timestamp.js
dojox.cometd._extendOutList.push(function(msg){
msg.timestamp = new Date().toUTCString();
return msg
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -