public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
@{
Layout = null;
}
!DOCTYPE html>
html>
head>
title>Index/title>
script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">/script>
script language="javascript">
var req = false;
var lastDelimiterPosition = -1;
$(document).ready(function () {
getData();
});
function getData() {
loadXMLDoc("/Comet/Test");
}
//新建一个XHR
function createRequest() {
if (window.XMLHttpRequest !(window.ActiveXObject)) {
try {
req = new XMLHttpRequest();
} catch (e) {
req = false;
} // branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
req = false;
}
}
}
}
//发起请求
function loadXMLDoc(url) {
try {
if (req) {
req.abort();
req = false;
}
createRequest();
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send("");
} else {
alert('unable to create request');
}
} catch (e) { alert(e.message); }
}
//检查状态
function processReqChange() {
if (req.readyState == 3) {
try {
ProcessInput(req.responseText);
if (req.responseText.length > 3000) {
lastDelimiterPosition = -1; getData();
}
}
catch (e) {
alert(e.message);
}
}
}
//拆分字符串
function ProcessInput(input) {
var text = input;
var nextDelimiter = text.indexOf('|', lastDelimiterPosition + 1);
if (nextDelimiter != -1) {
var timeStamp = text.substring(nextDelimiter + 1);
if (timeStamp.length > 0) {
lastDelimiterPosition = nextDelimiter;
ProcessTime(timeStamp);
}
}
}
//输出 或者触发什么事件
function ProcessTime(time) {
document.getElementById('div1').innerHTML = time;
}
/script>
/head>
body>
div>
div id="div1">
/div>
div id="div2">
/div>
/div>
/body>
/html>