回调到服务器 [英] Call Back to server

查看:150
本文介绍了回调到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,如果用户键入textbox,则该程序将session保持打开状态.下面是基本的ASP.NET代码:

I''m trying to create a program that keeps a session open if a user is typing in a textbox. Below is the basic ASP.NET code:

<asp:TextBox ID="txtMemoBox" runat="server" OnKeyUp="javascript:ProcessKeyStroke();"></asp:TextBox>
<asp:TextBox ID="hField" runat="server" Text="0"></asp:TextBox>



txtMemoBox是一个字段,当用户在其中键入内容时,我们希望保持session打开.

hField 将被隐藏,并用于计算txtMemoBox字段中每20次击键次数,以确定何时续订session.因此,如果在给定的会话时间内未检测到20次击键,则session将自动失效.

下面是javascript函数ProcessKeyStroke():



txtMemoBox is the field that when the user is typing within it we want to keep the session open.

hField will be hidden and is used to count every 20 keystroke within the txtMemoBox field to determine when to renew the session. Therefore if 20 keystrokes haven''t been detected within the given session time then the session will automatically expire.

Below is the javascript function ProcessKeyStroke():

function ProcessKeyStroke()
{
  document.getElementById(''<%=hField.ClientID%>'').value = parseFloat(document.getElementById(''<%=hField.ClientID%>'').value) + 1;

            if(document.getElementById(''<%=hField.ClientID%>'').value > 20)
            {
                document.getElementById(''<%=hField.ClientID%>'').value = 0;

                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                    xmlhttp = new ActiveXObject();
                }
            }
        }



我知道XMLHttpRequest()允许调用和接收来自服务器的单个控件.我的问题是XMLHttpRequest()ActiveXObject()单独与服务器通信并因此续签session?



I understand that XMLHttpRequest() allows calls to and from the server for individual controls. My question is does XMLHttpRequest() and ActiveXObject() alone communicate with the server and therefore renew the session?

推荐答案

本文对您有帮助吗?

http://ajaxpatterns.org/archive/Timeout.php [
Does this article help?

http://ajaxpatterns.org/archive/Timeout.php[^]


我认为您想研究的是心跳模式

http://ajaxpatterns.org/Heartbeat [ ^ ]

因此,您将拥有一些侦听请求的服务器端代码,也许是一个名为Heartbeat的Web服务,并带有一种名为"Pulse"的方法

您的客户端代码将定期调用此代码,以告知服务器仍在这里!"

注意:您需要仔细实施此操作.也许有些脚本记录最后一次按键"的时间,并且仅在可接受的范围内时才调用Pulse方法.否则,如果您每隔30秒调用一次,则用户可能会使浏览器处于打开状态&去度假他们的会议将保持开放.

另一个例子

http://lamahashim.blogspot.com/2009/06/heart-beat- design-pattern-keeping.html [ ^ ]
I think what you want to look into is the heartbeat pattern

http://ajaxpatterns.org/Heartbeat[^]

So you''d have some server side code that listens for requests, maybe a webservice called Heartbeat with a method called ''Pulse''

You client side code would periodically call this code to tell the server ''still here!''

NB: You need to implement this carefully. Maybe some script to record ''last key press'' time and only call the Pulse method when this is within acceptable range. Otherwise, if you just called it every 30 seconds or whatever, a user could leave the browser open & go on holiday & their session would stay open.

Another example

http://lamahashim.blogspot.com/2009/06/heart-beat-design-pattern-keeping.html[^]


在浏览器端使用jQuery可能是一个好主意:
http://api.jquery.com/jQuery.ajax/ [ jQuery计时器 [ jQuery数据 [ ^ ]允许您将任何类型的数据附加到DOM元素.

只是一些想法
Espen Harlinn
I might be a good idea to use jQuery on the browser side:
http://api.jquery.com/jQuery.ajax/[^].

A timer feature, to keep the session alive, can be implemented using jQuery Timers[^]

jQuery Data[^] allows you to attach data of any type to DOM element.

Just a few thoughts
Espen Harlinn


这篇关于回调到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆