当客户端需要定时轮询服务器获取一些消息的时候,可以使用 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. pycharm上安装使用easygui

    运行下面两句后,显示错误 import easygui easygui.msgbox("Hello There!") 错误: Traceback (most recent call ...

  2. 模拟搭建Web项目的真实运行环境(四)

    本篇介绍如何部署mongodb环境,主要分为三个部分: 第一部分 介绍如何在ubuntu下安装mongodb, 第二部分 介绍如何在windows下安装使用MongoChef客户端, 第三部分 介绍在 ...

  3. 惊闻Java要收费之后

    今天看到朋友圈里的文章 <Oracle终于要向Java的非付费用户开枪了>,被这个标题吓了一跳,还以为Java要全面收费了.又被标题党骗了. 但是仔细想想,以Oracle公司的尿性,没准哪 ...

  4. SortedList和HashTable

    都是集合类,C#中同属命名空间System.Collections,“用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,同时key是区分大小写:value用于存储对应于key的 ...

  5. NYOJ之题目325 zb的生日

    -------------------------------------- 刷一辈子水题... AC代码: import java.util.Scanner; public class Main { ...

  6. NoSQL与RDBMS的九点区别联系

    原文链接:http://blog.sina.com.cn/s/blog_5373fb0b0101ft8a.html     1 理解ACID与BASE的区别(ACID是关系型数据库强一致性的四个要求, ...

  7. 给DOM生成的元素添加事件

    问题:通过js给页面添加新元素,并给该元素添加绑定事件,但新添加的元素上却没有绑定任何事件. 常见例子:在处理表格的时候,每行行末有个删除按钮,如下图.点击删除按钮的时候删除这一行. //html部分 ...

  8. python学习笔记-进程线程

    1.什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述 ...

  9. java反射详解

    本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的,要不然只看理论的话,看了也不懂,不过建议大家在看完文章之后,在回过头去看看理论,会有更好的理解. 下面开始正文. [案例1]通过一个对象 ...

  10. Linux 线程管理

    解析1 LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait()函数.但这个函数的执行过程比较难于理解. pthread_cond_wait()的工 ...