有時(shí)候你愿望你的頁(yè)面“一直活著”。也就是說(shuō),如果一個(gè)用戶填寫一個(gè)龐雜的表單,在用戶實(shí)現(xiàn)之前。你必定不盼望session過(guò)期。否者用戶可能因而變得十分憤怒。
有"心跳"的,輸出如下: 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 這樣看起來(lái)客戶端閑置的時(shí)候,session依然活著,也就是網(wǎng)站“心跳”著。 (有點(diǎn)扯淡) Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 <system.web>2 <sessionState timeout="2"> 3 </sessionState> 4 </system.web> 下面是具體步驟:因?yàn)槲覀冃枰诜?wù)端有一個(gè)方法供客戶端調(diào)用。故使用一個(gè)WebMethod方法。 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 . . . (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 public partial class _Default : System.Web.UI.Page 2 { 3 [WebMethod(EnableSession=true ) ] 4 public static void PokePage() 5 { 6 // called by client to refresh session 7 MiscUtilities.ODS("Server: I am poked"); 8 } 9 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 } 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 --> 咱們須要有一個(gè)客戶端的JavaScript定時(shí)地去調(diào)用服務(wù)真?zhèn)€辦法。 為了追蹤詳細(xì)產(chǎn)生了什么,應(yīng)用一個(gè)公用的函數(shù)ODS(在MiscUtilities類中) 不"心跳"的,輸入如下: 為了察看session的狀況事件,我在global.asax中增加用于調(diào)試的字符串。 Code highlighting produced by Actipro CodeHighlighter (freeware) -->1 <asp:ScriptManager ID="ScriptManager1" runat="server"2 EnablePageMethods="true"> 3 </asp:ScriptManager> 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 } 7 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 相關(guān)的主題文章:
|