当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 关于.NET中的Server push技术

ASP.NET
ASP.NET在上传文件时对文件类型的高级判断的代码
JQuery运用ajax注册用户实例(后台asp.net)
Asp.net与SQLserver一起打包部署安装图文教程
asp.net 上传下载输出二进制流实现代码
asp.net(C#)解析Json的类代码
asp.net 截取字符串代码
asp.net ubb使用代码
asp.net XML文件操作实现代码
asp.net利用HttpModule实现防sql注入
ASP.NET(C#)中操作SQLite数据库实例
asp.net(c#)ref,out ,params的区别
asp.net(C#)防sql注入组件的实现代码
asp.net FCKeditor自定义非空验证
Asp.net TreeView来构建用户选择输入的方法 推荐
asp.net(C#)函数对象参数传递的问题
Asp.net中的GridView导出遇到的两个问题和解决方法
asp.Net 中获取一周第一天,一月第一天等实现代码
asp.net MaxLengthValidator 最大长度验证控件代码
C# 通用文件上传类
asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图

ASP.NET 中的 关于.NET中的Server push技术


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 92 ::
收藏到网摘: n/a

今天突发奇想,想研究一下服务端内容有变化,如何引起客户端的改变。

一般来说方法有2种,一种就是客户端用JS异步定时轮询服务器端,这种是大部分人采用的方法,但是我在想到底可以不可以实现服务器端有改变时才会主动推送到客户端呢?按理来说这种方式对于B/S是不可能实现的,因为B/S是无连接的,这种request/response的方式无法保持状态。搜了一下,看到博客园有位朋友写了篇文章是关于Server Push的,文章点这里看,我自己把它的代码复制过来看了下效果,完整代码如下:

前台:

以下为引用的内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ServerPushDemo.aspx.cs" Inherits="WebDemo.ServerPush.ServerPushDemo" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6 <head runat="server">
 7     <title>无标题页</title>
 8     <script type="text/javascript">
 9         function SetValue(time)
10 
        {
11             document.getElementById("TbTime").value=
time;
12 
        }
13     </script>

14 </head>
15 <body>
16     <form id="form1" runat="server">
17     <div>
18         <input type="text" id="TbTime"/>
19     </div>
20     </form>
21 </body>
22 </html>
23 

后台代码:

以下为引用的内容:

using System;
using
 System.Collections;
using
 System.Configuration;
using
 System.Data;
using
 System.Linq;
using
 System.Web;
using
 System.Web.Security;
using
 System.Web.UI;
using
 System.Web.UI.HtmlControls;
using
 System.Web.UI.WebControls;
using
 System.Web.UI.WebControls.WebParts;
using
 System.Xml.Linq;
using
 System.Threading;

namespace
 WebDemo.ServerPush
{
    
public partial class
 ServerPushDemo : System.Web.UI.Page
    {
        
protected void Page_Load(object
 sender, EventArgs e)
        {

        }

        
protected override void
 Render(HtmlTextWriter writer)
        {
            
base
.Render(writer);
            Response.Buffer 
= true
;
            
bool isOutput = false
;
            Response.Write(
""
);

            Response.Flush();
            
int lastsecond = 0
;
            
while
 (Response.IsClientConnected)
            {
                Thread.Sleep(
300
);
                
if (DateTime.Now.Second != lastsecond && !
isOutput)
                {

                    lastsecond 
=
 DateTime.Now.Second;
                    Response.Write(
"<script>SetValue('" + DateTime.Now.ToString() + "')\n </script> "
);
                    Response.Flush();
                    isOutput 
= true
;
                }
                
else

                {
                    isOutput 
= false;
                }

            }

        }
    }
}

这种方式的实现原理其实就是在OnRender事件里,用循环挂起连接,因为服务器端没有response完毕,所以一直和客户端保持连接,虽然这种方式能实现服务器端向客户端的推送,但它的代价太大,因为web的优势就是无连接。这样每个客户端都要占用服务器端的一个IIS连接,如果用户超过百个,就会非常恐怖的。所以这种方式我觉得不可取,相比来说,还是用AJAX的方式定时轮询更好。

真正意义上的Server Push好像我找了一些文章在Cgi里可以实现,点这里看。但在.net里的实现我就不得而知了,知道的高人请指点下,呵呵。

我还搜到一个关于.net推技术的就是Comet框架,可以实现长连接的,具体文章请点这里。改天我会把相关Demo发出来.