检测用户 Activity 以保持会话活动策略 (RxJS)

Detect User Activity to Keep Session Alive Strategy (RxJS)

我想让用户会话保持活动状态,用户 activity 是这样定义的。

The information system shall initiate locking of an interactive session after a 15 minute period of user inactivity (inactivity defined as no keyboard or mouse activity).

这就是我想出的..和往常一样,在 RxJS 中有很多方法可以完成某些事情,我想知道是否有更好的方法或标准策略

如果每 60 秒有用户 activity,下面的示例将 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});

既然没有人关注这个问题,我不妨留下我的最终答案。

      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});
      });