我想通过这样定义的用户活动来保持用户会话的活力。
信息系统应启动互动的锁定 在用户闲置15分钟(闲置)后的会话 定义为没有键盘或鼠标活动)。
这是我想出的..像往常一样,有很多方法可以在RxJS中完成某件事,我想知道是否有更好的方法或标准策略
下面的示例将在每60秒进行一次用户活动的情况下对服务器执行ping操作
const userActivityEvents = [
fromEvent(document, 'click'),
fromEvent(document, 'wheel'),
fromEvent(document, 'scroll'),
fromEvent(document, 'keypress'),
fromEvent(document, 'mousemove'),
fromEvent(document, 'touchmove'),
];
merge(...userActivityEvents).pipe(
throttleTime(60 * 1000),
switchMap(() => this.apiService.get('/api/auth/ping', new HttpParams(), new HttpHeaders({ignoreLoadingBar: ''})),
),
).subscribe({error: () => undefined});
答案 0 :(得分:0)
由于对此没有牵引力,所以我最好留下我的最终答案。
this.ngZone.runOutsideAngular(() => {
merge(
fromEvent(document, 'click'),
fromEvent(document, 'wheel'),
fromEvent(document, 'scroll'),
fromEvent(document, 'keypress'),
fromEvent(document, 'mousemove'),
fromEvent(document, 'touchmove'),
).pipe(
throttleTime(USER_ACTIVITY_POLL_TIME),
skip(1),
filter(() => !this.sessionModal),
filter(() => this.authService.authenticated),
switchMap(() => this.authService.ping(new HttpHeaders({ignoreLoadingBar: ''}))),
).subscribe({error: () => undefined});
});