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

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

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


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