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

ASP.NET
从一个舆论调查的制作谈面向对象的编程思路(二)
从一个舆论调查的制作谈面向对象的编程思路(三)
从一个舆论调查的制作谈面向对象的编程思路(四)
从一个舆论调查的制作谈面向对象的编程思路(五)
c#写的五子棋程序,供学习WinForms的鼠标事件和使用GDI+
我对.Net技术中asp.net应用的一点看法
ASP.NET连SQL7接口源代码
ASP.NET中密码保护,MD5和SHA1算法的使用
微软的远程处理框架.NET Remoting - 1
微软的远程处理框架.NET Remoting - 2
黑客写出了针对.Net平台的反编译器
在config.web中保存数据库连接串
在ASP.Net中两种利用CSS实现多界面的方法
ASP.NET中如何调用存储过程
ASP.NET中取代ASP的RS(Remote Scripting)技术的Framework
刚学ASP.Net,学了个简单的计算器
ASP.NET 链接数据库基础
一个简单的加密/解密方法
加入身份验证信息的SMTP mail发送
webconfig的设置节点说明

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


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