gwt 中的会话管理
我正在开发 GWT-ext 应用程序。 在此应用程序中,我管理客户端会话。为此,我编写了以下代码:
管理会话: 导入 com.google.gwt.user.client.Timer;
public class ClientTimers {
private static final Timer SESSION_MAY_HAVE_EXPIRED_TIMER = new Timer() {
@Override
public void run() {
// Warn the user, that the session may have expired.
// You could then show a login dialog, etc...
}
};
public static void renewSessionTimer() {
// First cancel the previous timer
SESSION_MAY_HAVE_EXPIRED_TIMER.cancel();
// Schedule again in 5 minutes (maybe make that configurable?)
// Actually, let's subtract 10 seconds from that, because our timer
// won't be synchronized perfectly with the server's timer.
SESSION_MAY_HAVE_EXPIRED_TIMER.schedule(5 * 60 * 1000 - 10000);
}
}
要获取用户活动:
Ext.get("pagePanel").addListener("click", new EventCallback() {
@Override
public void execute(EventObject e) {
//MessageBox.alert("On Mouse Click");
});
Ext.get("pagePanel").addListener("keydown", new EventCallback() {
@Override
public void execute(EventObject e) { //
//MessageBox.alert("On Key Press Click");
}
});
此代码工作正常,但我的问题是: 当超时发生时,此代码将自动注销。对于我的代码,我希望在单击或按下按键时它应该注销。情况是这样的:如果用户已登录,注销时间为 5 分钟。用户现在不在屏幕上执行任何活动,按照上面的代码,它将在 5 分钟完成后自动注销。
现在我的要求是,如果用户登录并且在 5 分钟内没有执行任何操作。它不应该自动注销。而不是在完成 5 分钟后注销,如果用户在 6 分钟内单击或按下按键那么它应该执行注销过程。
基本上,当计时器超过指定时间时,注销过程应该在用户活动上完成,而不是自动完成。
I am working on an GWT-ext application.
In this application I managed client side session. For this I write below code:
To manage the session:
import com.google.gwt.user.client.Timer;
public class ClientTimers {
private static final Timer SESSION_MAY_HAVE_EXPIRED_TIMER = new Timer() {
@Override
public void run() {
// Warn the user, that the session may have expired.
// You could then show a login dialog, etc...
}
};
public static void renewSessionTimer() {
// First cancel the previous timer
SESSION_MAY_HAVE_EXPIRED_TIMER.cancel();
// Schedule again in 5 minutes (maybe make that configurable?)
// Actually, let's subtract 10 seconds from that, because our timer
// won't be synchronized perfectly with the server's timer.
SESSION_MAY_HAVE_EXPIRED_TIMER.schedule(5 * 60 * 1000 - 10000);
}
}
To get the user activity:
Ext.get("pagePanel").addListener("click", new EventCallback() {
@Override
public void execute(EventObject e) {
//MessageBox.alert("On Mouse Click");
});
Ext.get("pagePanel").addListener("keydown", new EventCallback() {
@Override
public void execute(EventObject e) { //
//MessageBox.alert("On Key Press Click");
}
});
This code is working fine but my issues :
This code will do log out automatically as the time out occurs.For my code I want that on click or key down it should do logout. Case is like this:If user is logged in and time to log out is 5 min.User don't do any activity on the screen than right now as per the above code it will log out automatically as the 5 min complete.
Now my requirement is that if user logged in and it doesn't do any thing for 5 mins.It should not do any logged out automatically.Instead of logging out on completion of 5 min,If user do click or key down on 6 min then it should do the log out process.
Basically the log out process as the timer exceed the specified time should be done on the user activity, not automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在计时器中,每秒增加一个变量。
当用户在 5 分钟或第 6 分钟后单击任何按钮时,检查计数器变量,如果该变量大于 6,则可以使用 Window.Location.reload(); 注销或重新加载()。
In the Timer, increment a variable for each second.
And when user click on any button after 5 minutes or on 6th minute than check the counter variable and if the variable is greater than 6 than you can use Window.Location.reload(); to logout or reload().
我认为您正在寻找的是:
Window.Location.reload();
使用计时器每隔几秒触发一次,因此用户总是看起来很活跃。
(顺便说一句,我从 gwt -ext 中的窗口关闭问题 得到了这个)
I think the thing you are searchin for is:
Window.Location.reload();
Fire it every few secons with a timer, so the user always apper to be active.
(Btw I have that from Window close issues in gwt -ext )
在覆盖整个区域的不可见 div 上安装 JavaScript 事件处理程序。如果它收到事件,则向服务器发送 AJAX 请求。
然后服务器就可以做任何它需要做的事情。在客户端,您可以等待 AJAX 请求的回复并显示“您已注销”。
这种方法有一个缺点:存储在会话中的对象将保持活动状态太长时间。因此,即使用户从未注销并且只是离开(或者他的浏览器崩溃),会话也将保持活动状态。
几天后,大量的死会话将会累积,导致您的服务器崩溃。
因此,更好的解决方案是自动注销用户,就像您已经并安装 AJAX 事件处理程序一样,如上所述,以便在用户返回浏览器时显示消息。
这样,您的服务器可以清理死会话,并且用户在可以阅读时(当他位于屏幕前时)会收到一条消息。
请注意,您无法区分点击鼠标的用户和清洁人员。
Install a JavaScript event handler on an invisible div that covers the whole area. If it gets an event, send an AJAX request to the server.
The server can then do whatever it needs to do. On the client side, you can wait for a reply from the AJAX request and display "You have been logged out".
There is one drawback of this approach: Objects stored in the session will be kept alive for too long. So even if the user never logs out and just walks away (or his browser crashes), the session will stay alive.
After a couple of days, so many dead sessions will accumulate that your server will crash.
So the better solution is to auto logout the user as you do already and install an AJAX event handler as described above to display a message when the user returns to the browser.
This way, your server can clean up dead sessions and the user gets a message when he can read it (when he is in front of the screen).
Note that you can't differentiate between the user and the cleaning crew hitting the mouse.