cometd.js

来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 531 行 · 第 1/2 页

JS
531
字号
		if (!this.phonyForm) {
			if (dojo.render.html.ie) {
				this.phonyForm = document.createElement("<form enctype='application/x-www-form-urlencoded' method='POST' style='display: none;'>");
				dojo.body().appendChild(this.phonyForm);
			} else {
				this.phonyForm = document.createElement("form");
				this.phonyForm.style.display = "none";
				dojo.body().appendChild(this.phonyForm);
				this.phonyForm.enctype = "application/x-www-form-urlencoded";
				this.phonyForm.method = "POST";
			}
		}
		this.phonyForm.action = url || cometd.url;
		this.phonyForm.target = this.rcvNodeName;
		this.phonyForm.setAttribute("target", this.rcvNodeName);
		while (this.phonyForm.firstChild) {
			this.phonyForm.removeChild(this.phonyForm.firstChild);
		}
		for (var x in content) {
			var tn;
			if (dojo.render.html.ie) {
				tn = document.createElement("<input type='hidden' name='" + x + "' value='" + content[x] + "'>");
				this.phonyForm.appendChild(tn);
			} else {
				tn = document.createElement("input");
				this.phonyForm.appendChild(tn);
				tn.type = "hidden";
				tn.name = x;
				tn.value = content[x];
			}
		}
		this.phonyForm.submit();
	};
	this.processBacklog = function () {
		while (this.backlog.length > 0) {
			this.sendMessage(this.backlog.shift(), true);
		}
	};
	this.sendMessage = function (message, bypassBacklog) {
		if ((bypassBacklog) || (this.connected)) {
			message.connectionId = this.connectionId;
			message.clientId = cometd.clientId;
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], method:"POST", mimetype:"text/json", content:{message:dojo.json.serialize([message])}};
			return dojo.io.bind(bindArgs);
		} else {
			this.backlog.push(message);
		}
	};
	this.startup = function (handshakeData) {
		dojo.debug("startup!");
		dojo.debug(dojo.json.serialize(handshakeData));
		if (this.connected) {
			return;
		}
		this.rcvNodeName = "cometdRcv_" + cometd._getRandStr();
		var initUrl = cometd.url + "/?tunnelInit=iframe";
		if (false && dojo.render.html.ie) {
			this.rcvNode = new ActiveXObject("htmlfile");
			this.rcvNode.open();
			this.rcvNode.write("<html>");
			this.rcvNode.write("<script>document.domain = '" + document.domain + "'");
			this.rcvNode.write("</html>");
			this.rcvNode.close();
			var ifrDiv = this.rcvNode.createElement("div");
			this.rcvNode.appendChild(ifrDiv);
			this.rcvNode.parentWindow.dojo = dojo;
			ifrDiv.innerHTML = "<iframe src='" + initUrl + "'></iframe>";
		} else {
			this.rcvNode = dojo.io.createIFrame(this.rcvNodeName, "", initUrl);
		}
	};
};
cometd.mimeReplaceTransport = new function () {
	this.connected = false;
	this.connectionId = null;
	this.xhr = null;
	this.authToken = null;
	this.lastTimestamp = null;
	this.lastId = null;
	this.backlog = [];
	this.check = function (types, version, xdomain) {
		return ((!xdomain) && (dojo.render.html.mozilla) && (dojo.lang.inArray(types, "mime-message-block")));
	};
	this.tunnelInit = function () {
		if (this.connected) {
			return;
		}
		this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"mime-message-block"}])});
		this.connected = true;
	};
	this.tunnelCollapse = function () {
		if (this.connected) {
			this.connected = false;
			this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/reconnect", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
		}
	};
	this.deliver = cometd.iframeTransport.deliver;
	this.handleOnLoad = function (resp) {
		cometd.deliver(dojo.json.evalJson(this.xhr.responseText));
	};
	this.openTunnelWith = function (content, url) {
		this.xhr = dojo.hostenv.getXmlhttpObject();
		this.xhr.multipart = true;
		if (dojo.render.html.mozilla) {
			this.xhr.addEventListener("load", dojo.lang.hitch(this, "handleOnLoad"), false);
		} else {
			if (dojo.render.html.safari) {
				dojo.debug("Webkit is broken with multipart responses over XHR = (");
				this.xhr.onreadystatechange = dojo.lang.hitch(this, "handleOnLoad");
			} else {
				this.xhr.onload = dojo.lang.hitch(this, "handleOnLoad");
			}
		}
		this.xhr.open("POST", (url || cometd.url), true);
		this.xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		dojo.debug(dojo.json.serialize(content));
		this.xhr.send(dojo.io.argsFromMap(content, "utf8"));
	};
	this.processBacklog = function () {
		while (this.backlog.length > 0) {
			this.sendMessage(this.backlog.shift(), true);
		}
	};
	this.sendMessage = function (message, bypassBacklog) {
		if ((bypassBacklog) || (this.connected)) {
			message.connectionId = this.connectionId;
			message.clientId = cometd.clientId;
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], method:"POST", mimetype:"text/json", content:{message:dojo.json.serialize([message])}};
			return dojo.io.bind(bindArgs);
		} else {
			this.backlog.push(message);
		}
	};
	this.startup = function (handshakeData) {
		dojo.debugShallow(handshakeData);
		if (this.connected) {
			return;
		}
		this.tunnelInit();
	};
};
cometd.longPollTransport = new function () {
	this.connected = false;
	this.connectionId = null;
	this.authToken = null;
	this.lastTimestamp = null;
	this.lastId = null;
	this.backlog = [];
	this.check = function (types, version, xdomain) {
		return ((!xdomain) && (dojo.lang.inArray(types, "long-polling")));
	};
	this.tunnelInit = function () {
		if (this.connected) {
			return;
		}
		this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"long-polling"}])});
		this.connected = true;
	};
	this.tunnelCollapse = function () {
		if (!this.connected) {
			this.connected = false;
			dojo.debug("clientId:", cometd.clientId);
			this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/reconnect", connectionType:"long-polling", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
		}
	};
	this.deliver = cometd.iframeTransport.deliver;
	this.openTunnelWith = function (content, url) {
		dojo.io.bind({url:(url || cometd.url), method:"post", content:content, mimetype:"text/json", load:dojo.lang.hitch(this, function (type, data, evt, args) {
			cometd.deliver(data);
			this.connected = false;
			this.tunnelCollapse();
		}), error:function () {
			dojo.debug("tunnel opening failed");
		}});
		this.connected = true;
	};
	this.processBacklog = function () {
		while (this.backlog.length > 0) {
			this.sendMessage(this.backlog.shift(), true);
		}
	};
	this.sendMessage = function (message, bypassBacklog) {
		if ((bypassBacklog) || (this.connected)) {
			message.connectionId = this.connectionId;
			message.clientId = cometd.clientId;
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], method:"post", mimetype:"text/json", content:{message:dojo.json.serialize([message])}, load:dojo.lang.hitch(this, function (type, data, evt, args) {
				cometd.deliver(data);
			})};
			return dojo.io.bind(bindArgs);
		} else {
			this.backlog.push(message);
		}
	};
	this.startup = function (handshakeData) {
		if (this.connected) {
			return;
		}
		this.tunnelInit();
	};
};
cometd.callbackPollTransport = new function () {
	this.connected = false;
	this.connectionId = null;
	this.authToken = null;
	this.lastTimestamp = null;
	this.lastId = null;
	this.backlog = [];
	this.check = function (types, version, xdomain) {
		return dojo.lang.inArray(types, "callback-polling");
	};
	this.tunnelInit = function () {
		if (this.connected) {
			return;
		}
		this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/connect", clientId:cometd.clientId, connectionType:"callback-polling"}])});
		this.connected = true;
	};
	this.tunnelCollapse = function () {
		if (!this.connected) {
			this.connected = false;
			this.openTunnelWith({message:dojo.json.serialize([{channel:"/meta/reconnect", connectionType:"long-polling", clientId:cometd.clientId, connectionId:this.connectionId, timestamp:this.lastTimestamp, id:this.lastId}])});
		}
	};
	this.deliver = cometd.iframeTransport.deliver;
	this.openTunnelWith = function (content, url) {
		var req = dojo.io.bind({url:(url || cometd.url), content:content, mimetype:"text/json", transport:"ScriptSrcTransport", jsonParamName:"jsonp", load:dojo.lang.hitch(this, function (type, data, evt, args) {
			cometd.deliver(data);
			this.connected = false;
			this.tunnelCollapse();
		}), error:function () {
			dojo.debug("tunnel opening failed");
		}});
		this.connected = true;
	};
	this.processBacklog = function () {
		while (this.backlog.length > 0) {
			this.sendMessage(this.backlog.shift(), true);
		}
	};
	this.sendMessage = function (message, bypassBacklog) {
		if ((bypassBacklog) || (this.connected)) {
			message.connectionId = this.connectionId;
			message.clientId = cometd.clientId;
			var bindArgs = {url:cometd.url || djConfig["cometdRoot"], mimetype:"text/json", transport:"ScriptSrcTransport", jsonParamName:"jsonp", content:{message:dojo.json.serialize([message])}, load:dojo.lang.hitch(this, function (type, data, evt, args) {
				cometd.deliver(data);
			})};
			return dojo.io.bind(bindArgs);
		} else {
			this.backlog.push(message);
		}
	};
	this.startup = function (handshakeData) {
		if (this.connected) {
			return;
		}
		this.tunnelInit();
	};
};
cometd.connectionTypes.register("mime-message-block", cometd.mimeReplaceTransport.check, cometd.mimeReplaceTransport);
cometd.connectionTypes.register("long-polling", cometd.longPollTransport.check, cometd.longPollTransport);
cometd.connectionTypes.register("callback-polling", cometd.callbackPollTransport.check, cometd.callbackPollTransport);
cometd.connectionTypes.register("iframe", cometd.iframeTransport.check, cometd.iframeTransport);
dojo.io.cometd = cometd;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?