如何追踪用户在线状态?

发布于 2024-08-15 01:38:02 字数 297 浏览 7 评论 0原文

我想捕获以下参数:

lastAccessedTime - 用户上次访问站点的时间(通常在登录过程中显示)

isOnline - 表示用户是否在线的布尔值。

一个。将这些变量作为用户表本身的一部分是否有意义,还是应该通过单独的用户审核表来处理?

b.如果某些 SOAP / REST API 通过 API 调用公开功能,您如何跟踪上述参数(例如,在这种情况下您会修改 lastAccessedTime - 如果用户登录到门户,这可能会让用户感到困惑,isOnline 位也没有意义)如果用户进行 API 调用)。

I would like to capture the following parameters:

lastAccessedTime - The time when the user visited the site the last time (usually shown during the login process)

isOnline - A boolean to represent if a user is online or not.

a. Would it make sense to have these variables as part of the User table itself or should this be handled via a separate user audit table?

b. If certain SOAP / REST API's expose the functionality via API calls, how do you track the above parameters (e.g. Would you modify the lastAccessedTime in such cases - this might confuse the user if he logs into the portal, isOnline bit also will not make sense if the user does API calls).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她如夕阳 2024-08-22 01:38:02

我将创建一个链接回用户的会话表。我只需对过去 x 时间内处于活动状态的会话运行查询,而不是使用 isOnline 字段。我还会根据每个请求更新该会话字段,即使该请求是通过 API 发出的。

这确实会在修剪会话表时产生一些开销,但您也不会用无法修剪的非用户信息使用户表变得混乱。

I would create a session table that links back to the user. Instead of an isOnline field, I would just run a query for sessions that have been active within the last x amount of time. I would also update that session field with each request, even if that request is coming through an API.

This does create some overhead in pruning the session table, but you also don't clutter up your user table with non-user information, which can't be pruned.

你的背包 2024-08-22 01:38:02

将lastTimeActive设置为用户表中的一个字段,并在每次访问页面时更新它。您的“在线用户”列表是lastTimeActive 在5 分钟内的所有用户。

Make the lastTimeActive a field in the user table, and update it with each page access. Your "Users Online" list is all users whose lastTimeActive is within 5 minutes.

风筝在阴天搁浅。 2024-08-22 01:38:02

我会创建另一个表(userid,lastTimeActive),并频繁更新&检查表。

// update
update onlineusers set lastTimeActive = getdate() where userid=1234

// check
delete from onlineusers where lastTimeActive < dateadd(minute,-5,getdate())

I would create another table (userid, lastTimeActive), and frequently update & check the table.

// update
update onlineusers set lastTimeActive = getdate() where userid=1234

// check
delete from onlineusers where lastTimeActive < dateadd(minute,-5,getdate())
亽野灬性zι浪 2024-08-22 01:38:02

通过 HTTP 跟踪用户状态(在线/离线)的最大问题是如何确定用户何时离线。

很容易确定用户何时上线 - 仅存在经过身份验证的请求就假定用户处于活动状态。但是,由于 HTTP 是无状态的,因此缺少后续请求可能意味着用户离线,或者用户在线,但最近没有对您的应用程序执行任何特定操作。

因此,您可以做出的最佳猜测是超时,如果用户在超时期间没有发出请求,则切换到离线状态。

正如乔纳森·桑普森(Jonathan Sampson)建议的那样,最简单的实现是有一个lastTimeActive。但是,这不会为您提供用户会话的长度,而只能提供此时在线人数的近似值。

更复杂的方法是使用lastTimeActive 和lastTimeLoggedIn。 LastTimeLoggedIn 在第一次身份验证请求时设置,距离上一次身份验证请求超过 5 分钟。如果在过去五分钟内有经过身份验证的请求,则用户被视为在线。用户的会话长度是lastTimeActive 和lastTimeLoggedIn 之间的时间差。

如果您的应用程序还向用户提供注销的选择,您应该将该操作视为离线。但是,除非您的应用程序是银行应用程序,否则用户很可能会关闭浏览器。

另外,请避免使用任何后台线程来更新用户的离线/在线状态。仅当存在有关特定用户状态的显式请求时,您才应运行上述逻辑,并且应仅更新所要求的用户。

The biggest problem with tracking user presence (onine/offline) over HTTP is how to determine when the user has gone offline.

It's easy to determine when the user has come online - the mere presence of an authenticated request assumes that the user is active. However, since HTTP is stateless, the lack of a subsequent request can mean either that the user is gone offline, or that the user is online, but just hasn't done anything specific with your app recently.

Thus the best guess you can make is to have a timeout and if the user has not made a request during that timeout, to switch to offline state.

The simplest implementation would be to have a lastTimeActive, as Jonathan Sampson suggested. However, this won't give you the length of the user session, only an approximation of who's online at this moment.

More complex approach would be to have lastTimeActive and lastTimeLoggedIn. LastTimeLoggedIn is set at the time of first auth request that is more than 5 minutes from a previous auth request. A user is considered online, if there was an authenticated request in the last five minutes. The session length for the user is the time difference between lastTimeActive and lastTimeLoggedIn.

If your app also offers the choice of logging out to the user, you chouls consider that action also as going offline. However, unless your app is a banking app, chances are the users will just close their browser.

Also, avoid any background threads for updating the offline/online status of your users. You should be running the logic above only when there's an explicit request about the status of particular user and you should be updating only the users you were asked for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文