当客户端需要定时轮询服务器获取一些消息的时候,可以使用 servser-sent events

  .NET 服务端:  

         public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/event-stream";
context.Response.Write("retry: 1000\n");//告诉客户端请求的时间间隔
context.Response.Write("data: " + DateTime.Now.ToString() + " \n\n");//返回的数据格式 data:...
context.Response.Flush();
}

  客户端:  

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
function listener(event) {
var div = document.getElementById("main");
div.innerHTML += event.data +"<br/>";
} var es = new EventSource("/SSE_Server.ashx"); es.addEventListener("open", function (e) {
var msg = "";
if (e.target.readyState == EventSource.CONNECTING) {
msg = "链接中...";
} else if (e.target.readyState == EventSource.OPEN) {
msg = "OPEN";
} else if (e.target.readyState == EventSource.CLOSED) {
msg = "CLOSED";
}
var div = document.getElementById("main");
div.innerHTML += msg + "<br/>";
});
es.addEventListener("message", listener);
es.addEventListener("error", function (e) {
var msg ="";
if(e.target.readyState==EventSource.CONNECTING)
{
msg = "等待链接";
} else if (e.target.readyState == EventSource.CLOSED)
{
msg = "链接失败";
}
var div = document.getElementById("main");
div.innerHTML += msg + "<br/>";
});
</script>
</head>
<body>
<div id="main">
</div>
</body>
</html>

  效果展示:

  

附:

服务器可返回下列字段格式:

 Fields

The following field names are defined by the specification: event
  The event's type. If this is specified, an event will be dispatched on the browser to the listener for the specified event name;
  the web site source code should use addEventListener() to listen for named events. The onmessage handler is called if no event name is specified for a message.
data
  The data field for the message. When the EventSource receives multiple consecutive lines that begin with data:, it will concatenate them, inserting a newline character between each one.
  Trailing newlines are removed.
id
  The event ID to set the EventSource object's last event ID value.
retry
  The reconnection time to use when attempting to send the event. [What code handles this?] This must be an integer, specifying the reconnection time in milliseconds.
If a non-integer value is specified the field is ignored.

SSE: server-sent events的更多相关文章

  1. Play Framework, Server Sent Events and Internet Explorer

    http://www.tuicool.com/articles/7jmE7r Next week I will be presenting at Scala Days . In my talk I w ...

  2. server sent events

    server sent events server push https://html5doctor.com/server-sent-events/ https://developer.mozilla ...

  3. 【HTML5】浅析html使用SSE(Server-Sent Events)连接JSP

    目录结构: // contents structure [-] 关于SSE的一些话 什么是SSE SSE的浏览器支持情况 SSE的工作机制 使用SSE连接JSP文件 HTMl页面 服务器端 错误 错误 ...

  4. SQL Server Extended Events 进阶 3:使用Extended Events UI

    开始采用Extended Events 最大的阻碍之一是需要使用Xquery和XML知识用来分析数据.创建和运行会话可以用T-SQL完成,但是无论使用什么目标,数据都会被转换为XML.这个限制在SQL ...

  5. SQL Server Extended Events 进阶 2:使用UI创建基本的事件会话

    第一阶中我们描述了如何在Profiler中自定义一个Trace,并且让它运行在服务器端来创建一个Trace文件.然后我们通过Jonathan Kehayias的 sp_SQLskills_Conver ...

  6. SQL Server Extended Events 进阶 1:从SQL Trace 到Extended Events

    http://www.sqlservercentral.com/articles/Stairway+Series/134869/ SQL server 2008 中引入了Extended Events ...

  7. Comet之SSE(Server - Sent - Envent,服务器发送事件)

    1.SSE API 先要创建一个新的EventSource对象,并传进一个入口点: var source = new EventSource("myenvent.php"); △: ...

  8. ASP.NET Core的实时库: SignalR -- 预备知识

    大纲 本系列会分为2-3篇文章. 第一篇介绍SignalR的预备知识和原理 然后会介绍SignalR和如何在ASP.NET Core里使用SignalR. 本文的目录如下: 实时Web简述 Long ...

  9. SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events

    由于工作需要,决定深入研究SQL Server的扩展事件(Extended Events/xEvents),经过资料搜索,发现国外大牛的系列文章,作为“学习”阶段,我先翻译这系列文章,后续在工作中的心 ...

  10. Web端即时通讯技术盘点:短轮询、Comet、Websocket、SSE

    1. 前言 Web端即时通讯技术因受限于浏览器的设计限制,一直以来实现起来并不容易,主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Serve ...

随机推荐

  1. 用DllImport引用的外部DLL文件如何通过clickonce发布

    在.net中非托管代码或非COM组件的外部DLL文件只能用DllImport的方式使用,但是在clickonce方式发布时不会自动包含到发布清单中去,办法是在项目中先把外部DLL文件作为普通文件添加进 ...

  2. 9. 了解 Cocoa-百度百科

    Cocoa是苹果公司为Mac OS X所创建的原生面向对象的API,是Mac OS X上五大API之一(其它四个是Carbon.POSIX.X11和Java). 苹果的面向对象开发框架,用来生成 Ma ...

  3. 21. Merge Two Sorted Lists —— Python

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  4. 2016年6月20日 JAVA知识框架

    基于 J2EE 列举的知识架构,大体列举开发基础知识.帮助我随时查缺补漏,奉行好记性不如烂笔头.写了这该随笔,以便后续查询. 1  JAVA简介 2  JAVA编程环境 3  JAVA基本编程结构 4 ...

  5. PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置

    PhPStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具,PhpStorm可随时帮助用户对其编码进行调整,运行单元测试或者提供可视化debug功能.Phpstrom的一款名 ...

  6. 19.Java 注解

    19.Java注解 1.Java内置注解----注解代码 @Deprecated                                    //不推荐使用的过时方法 @Deprecated ...

  7. css雪碧图生成工具4.3更新

    v3.0更新介绍地址:http://www.cnblogs.com/wang4517/p/4476758.html v4.0更新介绍地址:http://www.cnblogs.com/wang4517 ...

  8. (转载)Win8.1的版本

    Win8.1版本到底有哪些? Windows 8.1的各版本有什么不同.差别和区别? Win8.1旗舰版有木有? Win8.1最好的版本是哪个? 我应该用哪个版本的Windows 8.1? 之前软媒曾 ...

  9. 使用base.调用父类里面的属性

    使用base.调用父类里面的属性public class parent { public string a; }public class child :parent { public string g ...

  10. ExtJS 中类的继承

    ExtJS 允许对现有的类进行扩展,其扩展可以通过继承来实现.接下来我们就对刚刚使用ExtJS定义的Person类进行继承,定义一个Developer类,它继承自Person,同时还拥有Coding方 ...