javascript EXT JS 会话超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5625091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:48:53  来源:igfitidea点击:

EXT JS Session Timeout

javascriptextjssession-timeout

提问by extjsnewbie

EXT JS - I would like to know how to check the json response for a session time out like if a user is idle for say 20 minutes or so if his session is expired or not

EXT JS - 我想知道如何检查会话超时的 json 响应,比如用户是否空闲了 20 分钟左右,如果他的会话是否过期

回答by Tommi

There is no standard way of handling session timeouts in ExtJS. ExtJS is a client-side library, used to create the user interface/front-end layer of an application, while session management takes place on the server side.

在 ExtJS 中没有处理会话超时的标准方法。ExtJS 是一个客户端库,用于创建应用程序的用户界面/前端层,而会话管理发生在服务器端。

ExtJS Ajax requests implement a callback mechanism. It means that a certain Javascript function is assigned as the callback function, which is called when the Ajax request has finished (either successfully or unsuccessfully). Here's an example taken from ExtJS API Documentation- see parameters success and failure that define the callback functions:

ExtJS Ajax 请求实现了回调机制。这意味着某个 Javascript 函数被指定为回调函数,当 Ajax 请求完成(成功或不成功)时调用该函数。这是从ExtJS API 文档中获取的示例- 请参阅定义回调函数的参数成功和失败:

// Basic request
Ext.Ajax.request({
   url: 'foo.php',
   success: someFn,
   failure: otherFn,
   headers: {
       'my-header': 'foo'
   },
   params: { foo: 'bar' }
});

So, in the case of session timeout, you could (for example) construct a JSON response, which would contain some error code (defined by you), and an error message to be shown to the user. The callback function should then check if this error is returned from the server, and take necessary actions (show error message, redirect to login page, etc.) when that happens.

因此,在会话超时的情况下,您可以(例如)构造一个 JSON 响应,其中将包含一些错误代码(由您定义)和要显示给用户的错误消息。然后回调函数应该检查此错误是否从服务器返回,并在发生这种情况时采取必要的操作(显示错误消息、重定向到登录页面等)。

Note that in the above case, from ExtJS viewpoint, the Ajax request would actually be successful. When the HTTP request fails altogether (HTTP errors like 403 and such), the Ajax request is considered unsuccessful. This is important because it is usually possible to define different callback functions for successful and unsuccessful requests (as in the above sample code).

请注意,在上述情况下,从 ExtJS 的角度来看,Ajax 请求实际上会成功。当 HTTP 请求完全失败(如 403 之类的 HTTP 错误)时,Ajax 请求被认为是不成功的。这很重要,因为通常可以为成功和不成功的请求定义不同的回调函数(如上面的示例代码)。

回答by Thiago Veronezi

You can mock the timeout session...

您可以模拟超时会话...

 var keepaliveHandler = new Ext.util.DelayedTask(function(){
    Ext.Ajax.request({
        url : '/keepalive',
        method : 'GET',
        success: function(response, options){
//dummy server call each 60 seconds
            keepaliveHandler.delay(60000);
        }
    });
});
var timeoutHandler = new Ext.util.DelayedTask(function(){
//invalidate session
    Ext.Ajax.request({
        url : '/logout',
        method : 'GET',
        success: function(response, options){
            Ext.MessageBox.show({
                title: MessagesMap.getMessage('session.closed'),
                msg: MessagesMap.getMessage('session.closed.message'),
                buttons: Ext.MessageBox.OK,
                fn: function() {
                    window.location.pathname = '/';                 
                },
                icon: Ext.MessageBox.WARNING
            });
        }
    });
});
if(Ext.ux.SystemProperties.isLogged) {
    keepaliveHandler.delay(60000);
    timeoutHandler.delay(Ext.ux.SystemProperties.timeout);
//check for mouse movements
    document.body.onmousemove = function(e) {
        timeoutHandler.delay(Ext.ux.SystemProperties.timeout);
    };  
}