1.JSONP跨域请求:
function getJSONP(url, callBack) {
var cbnum = "cb" + getJSONP.counter++;
var cbname = "getJSONP." + cbnum;
if (url.indexOf("?") === -1)
url += "?jsonp=" + cbnamme;
else
url += "&jsonp=" + cbname;
var script = document.createElement("script");
getJSONP[cbnum] = function(response) {
try {
callback(response);
}
finally {
delete getJONP[cbnum];
script.parentNode.removeChild[script];
}
};
script.src = url;
document.body.appendChild(script);
}
getJSONP.counter = 0;
2.Comet推:
var ticker = new EventSource("stockprices.php");
ticker.onmessage = function(e) {
var type = e.type;
var data = e.data;
};
兼容实现:
if (window.EventSource === undefined) {
window.EventSource = function(url) {
var xhr;
var evtsrc = this;
var charsReceived = 0;
var type = null;
var data = "";
var eventName = "message";
var lastEventId = "";
var retrydelay = 1000;
var aborted = false;
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
switch(xhr.readyState) {
case 3: processData(); break;
case 4: reconnect(); break;
}
};
connect();
function reconnect() {
if (aborted) return;
if (xhr.status >= 300) return;
setTimeout(connect, retrydelay);
};
function connect() {
charsReceived = 0;
type = null;
xhr.open("GET", url);
xhr.setRequestHeader("Cache-Control", "no-cache");
if (lastEventId) xhr.setRequestHeader("Last-Event-ID", lastEventId);
xhr.send();
};
function processData() {
if (!type) {
type = xhr.getResponseHeader("Content-Type");
if (type !== "text/event-stream") {
aborted = true;
xhr.abort();
return;
}
}
var chunk = xhr.responseText.substring(charsReceived);
charsReceived = xhr.responseText.length;
var lines = chunk.replace(/(\r\n|\r|\n)$/, "").split(/\r\n|\r|\n/);
for (var i = 0; i < lines.lenght; i++) {
var line = lines[i], pos = line.indexOf(":"), name, value="";
if (pos == 0) continue;
if (pos > 0) {
name = line.substring(0, pos);
value = line.substring(pos+1);
if (value.charAt(0) == " ") value = value.substring(1);
}
else name = line;
switch(name) {
case "event": eventName = value; break;
case "data": data += value + "\n"; break;
case "id": lastEventId = value; break;
case "retry": retrydelay = parseInt(value) || 1000; break;
default: break;
}
//空行发送
if (line === "") {
if (evtsrc.onmessage && data !== "") {
if (data.charAt(data.length - 1) == "\n")
data = data.substring(0, data.length - 1);
evtsrc.onmessage({type: eventName, data: data, origin: url});
}
data = "";
continue;
}
}
};
};
}