AngularJS机制向服务器询问基于令牌的状态

时间:2017-06-07 10:14:50

标签: angularjs html5

在我的客户端,我有一个触发api调用的按钮,需要很少的minuts来回复,所以在服务器端我想将一个令牌返回给客户端,然后客户端可以根据令牌请求状态

客户如何每1分钟询问一次状态可以说?而且我也不想使用数据库,只想将令牌保存在缓存中,我该怎么办呢?

thansk!

1 个答案:

答案 0 :(得分:0)

这可能不是最好或唯一的解决方案。但是我一直在使用这个库,它的优点和简单。

ngIdle可帮助您跟踪角度应用的时间,并在该时间过去时执行功能。这通常用于超时和空闲类型的东西,但我想你也可以使用它。

用法:

在您应用的配置中,将库初始化为:

 function ConfigFn(IdleProvider) {

        //set configuration for ng-idle
        IdleProvider.idle(60); //in seconds
        IdleProvider.timeout(120); //do not use this if not really required
}

AFAIK,它为您提供2次钩子,并在每次钩子时引发一个事件。您可以收听此活动并执行任何功能。

run区块中:

function runtimeFn(Idle) {
        //setting a timer watch as soon as application bootstraps, you have to reset it every time your time elapses and you call server.
        Idle.watch(); //Idle is just a service from the library

        //responsing to various events in the application
        $rootScope.$on('IdleStart', callServerForToken);
        $rootScope.$on('IdleTimeout', doSomethingAgainIfYouWant); //do not use this if not really required
    }

这应该对你有所帮助。如果有些事情仍然不清楚,请告诉我。