当前位置: 网站建设 > 网页设计 > 建站经验 >

让你的网站 心跳 起来

时间:2013-03-02 04:14来源:未知 作者:admin 点击:

标签:让你的网站 心跳 起来 你的(48)h(8)如下(8)跳的(1)输出(1)心跳(1)网站(482)code(3)起来(5)有心(1)
有心跳的,输出如下: Code highlighting produced by Actipro CodeHighlighter (freeware) -->1%@ApplicationLanguage=C#% 2scriptRunAt=server 3 4voidApplication_Start(objectsender,EventArgse) 5{ 6MiscUtilities.ODS(****ApplicationStart); 7} 8voidSession_Start(objectsender
让你的网站 心跳 起来》文章地址:http://www.tfxk.com/wangyesheji/jianzhanjingyan/030234T42013.htm

    有"心跳"的,输出如下:

Code highlighting produced by Actipro CodeHighlighter (freeware) --> 1 <%@ Application Language="C#" %>
 2 <script RunAt="server">
 3       
 4     void Application_Start(object sender, EventArgs e)
 5     {
 6         MiscUtilities.ODS("****ApplicationStart");
 7     ,让你永远受用的10个CSS简写技巧;}
 8     void Session_Start(object sender, EventArgs e)
 9     {
10         MiscUtilities.ODS("Session_Start");
11     }
12     void Session_End(object sender, EventArgs e)
13     {
14         MiscUtilities.ODS("Session_End");
15     } 
16 

    下面是具体步骤:因为我们须要在服务端有一个办法供客户端调用。故应用一个WebMethod方式。
1、在页面上咱们必需有一个ScriptManager
2、ScriptManager 的EnablePageMethods 必须设置成true
3、WebMethod 方法必须是public跟static的
4、WebMethod 方法必须将EnableSession属性设置成true

Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 // ---- ODS (Output Debug String) ----------------------
2 public static void ODS(string Msg)
3 {
4     String Out = String.Format("{0}  {1}", DateTime.Now.ToString("hh:mm:ss.ff"), Msg);
5     System.Diagnostics.Debug.WriteLine(Out);
6 }

    我们需要有一个客户端的JavaScript定时地去调用服务真个方法。

Code highlighting produced by Actipro CodeHighlighter (freeware) --> 1 10:26:06.10  ****ApplicationStart
 2 10:26:08.05  Session_Start
 3 Client: Poke Server
 4 10:26:18.93  Server: I am poked
 5 Client: Poke Server
 6 10:26:28.95  Server: I am poked
 7 Client: Poke Server
 8 10:26:38.96  Server: I am poked
 9 Client: Poke Server
10 10:26:48.98  Server: I am poked
11 
12 ,让Web应用程序飞起来的秘诀;    . . . (lines deleted)
13 
14 Client: Poke Server
15 10:29:59.45  Server: I am poked
16 Client: Poke Server
17 10:30:09.47  Server: I am poked
18 Client: Poke Server
19 10:30:19.48  Server: I am poked
20 
21     . . . (lines deleted)
22  Code highlighting produced by Actipro CodeHighlighter (freeware) --> 1 <script type="text/javascript">
 2     var HeartBeatTimer;
 3     function StartHeartBeat()
 4     {
 5         // pulse every 10 seconds
 6         if (HeartBeatTimer == null)
 7             HeartBeatTimer = setInterval("HeartBeat()", 1000 * 10);
 8     }
 9     function HeartBeat()
10     {
11         // note: ScriptManger must have: EnablePageMethods="true"
12         Sys.Debug.trace("Client: Poke Server");
13         PageMethods.PokePage();
14     }
15 <body id="MyBody"  onload="StartHeartBeat();">
16  Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 public partial class _Default : System.Web.UI.Page
2 {
3     [WebMethod(EnableSession=true ) ]
4     public static void PokePage()
5     {
6         // called by client ,让IE浏览器支持CSS3圆角属性的方法;to refresh session
7         MiscUtilities.ODS("Server: I am poked");       
8     }
9  Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 10:22:43.03 ****ApplicationStart 
2 10:22:45.13 Session_Start 
3 10:25:00.00 Session_End  Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 <asp:ScriptManager ID="ScriptManager1" runat="server" 
2     EnablePageMethods="true">
3 </asp:ScriptManager>

 

    这样看起来客户端闲置的时候,session依然活着,也就是网站“心跳”着。 (有点扯淡)

    有时候你愿望你的页面“一直活着”。也就是说,如果一个用户填写一个庞杂的表单,在用户实现之前。你必定不盼望session过期。否者用户可能因而变得十分愤怒。
    这也不是简略的加长session过时时间的事件。假如你这样做,当拜访页面的用户分开这个网站,session将会仍然存活在服务器的内存中很长一段时光。增添session过期时间是一个解决措施,然而它不是一个很好的解决方法。
    我的目的是:一旦网页在客户端被翻开,session就始终激活。即便不任何回发来重设session的时间,让DIV设置Float后高度不自动增加。当网页封闭的时候,session将天然停止。
    我这样来实现这个解决计划:在客户端每隔一段时间就去"ping"服务端,这个时间少于session的过期时间。这就是Heartbeat设计模式。
麻烦的设置:     ,让div+css的div居中而里面的文字不居中的做法;   为了到达测试的目标。我在web.config中设置session超时时间为2分钟。

Tag:网站   网站  

    为了追踪详细产生了什么,使用一个公用的函数ODS(在MiscUtilities类中)

    没有"心跳"的,输入如下:

--> [网站建设之]让你的网站"心跳"起来

Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 <system.web>
2   <sessionState timeout="2">
3   </sessionState>
4  </system.web>

    为了察看session的状况事件,我在global.asax中增加用于调试的字符串。


(责任编辑:网站建设)
让你的网站 心跳 起来相关文章
上一篇:让你永远受用的10个CSS简写技巧 下一篇:让你的网站大受欢迎必做的八个方面
回到顶部