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

ASP.NET
析构函数的作用 什么是析构函数
asp.net下Request.QueryString取不到值的解决方法
设置DropDownList的当前选项
生成多字段排序分页的SQL的通用类
注册表中存储数据库链接字符串的方法
asp.net下百度的编码和解码
asp.net 操作excel的实现代码
用ASP.NET做的个性化的邮件发送系统
DotNet2.0 生成网站的测试
ASP.net 验证码实现代码(C#)
C#中string与byte[]的转换帮助类-.NET教程,C#语言
把jQuery的each(callback)方法移植到c#中
asp.net对URL含有中文参数的转换
C#语言初级入门介绍
C#编码好习惯小结
C#声明方法实例说明
C#使用正则表达式实例
C#列出局域网中可用SQL Server服务器(续)
将DataRow转成指定类型的类,并返回这个类的对象(带值)
asp.net OleDbCommand 的用法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 126 ::
收藏到网摘: 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发出来.