关闭浏览器或选项卡时注销用户 [英] logout user when browser or tab is closed

查看:64
本文介绍了关闭浏览器或选项卡时注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个聊天网络应用程序.刚刚发现,如果用户直接关闭选项卡或浏览器而不单击注销"按钮,则数据库中的状态仍将显示为联机".那么,当浏览器或选项卡关闭时,如何注销用户呢?我使用的是COOKIE,而不是会话.我在堆栈溢出中进行了大量搜索,没有找到有效的解决方案.

I'm making a chat web app. Just found out that if user closes the tab or browser directly without clicking 'logout' button, the state in database will still show "online". So how to logout user when browser or tab is closed? I'm using COOKIE instead of session. I searched a lot in stack overflow, didn't find a working solution.

有人可以帮助我吗?预先感谢!

Anyone can help me? Thanks in advance!

推荐答案

评论中的很多人似乎都误解了您的问题.即使在您关闭浏览器时将删除会话Cookie,也不会告诉您的Web应用会话已结束.

A lot of people in the comments seem to have misunderstood your question. Even though session cookies will be deleted when you close your browser this doesn't tell your web app that the session is over.

可靠地检测用户是否打开您的Web应用程序的唯一真实方法是进行频繁的AJAX调用,以更新数据库条目,例如上次访问该用户的时间.如果他们错过了几个预期的AJAX呼叫,则可以认为它们处于脱机状态.

The only real way of reliably detecting if the user has your web app open is to make a frequent AJAX call that updates a database entry to say when the user was last seen. If they miss several expected AJAX calls you can assume they are offline.

另一种可能性是用于在断开连接时更新用户状态的网络套接字.

Another possibility is a web-socket that you use to update the users status on disconnect.

您可能仍然需要实施这些系统之一来获取新的聊天消息,因此您可能还需要集成一个系统来检查用户的最后访问时间.

You will probably need to implement one of these systems anyway to get new chat messages so you might as well just integrate a system for checking when the user was last seen in to that.

这篇关于关闭浏览器或选项卡时注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆