java - SPA: how to know if user's alive -
i have: spa application, means can offline-processing. means app ask server if needs additional info or wants save something.
i holds user connections in guava cache expired policy. means shouldn't worry session destruction after timeout.
crucial point: each time user request reset timeout avoid session destruction. when user inactive during specified period, guava cache throw session away.
problem: problem linked spa. spa, if user don't send requests doesn't mean he's inactive.
i want: automatically close user session log out him after timeout.
question: how can know if user active or not in spa?
the first comes in mind - use expired cache on server-side, , send each e.g. 5 minutes keep_alive request client indicate client active. seems overengineering. plus clog server hundreds of unnecessary requests.
so found (no mean pioneer) better solution: client calculate inactive period own, , send appropriate request if inactive_period greater timeout.
activitylistener = { timeout: null, activityhandler: function () { $.cookie('last_activity', new date().gettime()); }, initialize: function (timeout) { this.timeout = timeout; $.cookie('last_activity', new date().gettime()); if ($.cookie('do_activity_check') != true) { $.cookie('do_activity_check', true); setinterval(this.activitycheck, timeout / 2); } addeventlistener('click', this.activityhandler, false); addeventlistener('scroll', this.activityhandler, false); }, handletimeout: function () { if (ologinpage.authorities != null) { le.send({ "@class": "userrequest$logout", "id": "userrequest.logout" }); } }, activitycheck: function () { var after_last_activity_ms = new date().gettime() - $.cookie('last_activity'); if (after_last_activity_ms > activitylistener.timeout) activitylistener.handletimeout(); } }
Comments
Post a Comment