当浏览器关闭时运行服务器端函数

发布于 2024-07-13 00:29:47 字数 421 浏览 9 评论 0原文

背景:我正在使用 C# 代码隐藏创建一个非常简单的类似聊天室的 ASP.NET 页面。 当前用户/聊天消息显示在位于 AJAX 更新面板内的控件中,并使用计时器 - 它们每隔几秒从数据库中提取信息。

我试图找到一种简单的方法来处理将用户退出浏览器时的状态设置为“离线”,而不是点击“注销”按钮。 IsOnline 的“离线”状态目前仅为 1 个字符 (y/n)。

到目前为止,我已经使用 Javascript 研究了 window.onbeforeunload,并在此事件上设置了一个带有函数的隐藏表单变量 -> 当然,问题是,我仍然需要在代码隐藏的某个地方测试这个隐藏的表单变量来执行最终的服务器端数据库查询,从而有效地将用户设置为离线。

我可能完全混淆了这个可能简单的问题! 当然,我会很感激任何完全不同的替代建议。

谢谢

Background: I'm creating a very simple chatroom-like ASP.NET page with C# Code-Behind. The current users/chat messages are displayed in Controls located within an AJAX Update Panel, and using a Timer - they pull information from a DB every few seconds.

I'm trying to find a simple way to handle setting a User's status to "Offline" when they exit their browser as opposed to hitting the "Logoff" button. The "Offline" status is currently just a 1 char (y/n) for IsOnline.

So far I have looked into window.onbeforeunload with Javascript, setting a hidden form variable with a function on this event -> Of course the trouble is, I'd still have to test this hidden form variable in my Code-Behind somewhere to do the final Server-Side DB Query, effectively setting the User offline.

I may be completely obfusticating this likely simple problem! and of course I'd appreciate any completely different alternative suggestions.

Thanks

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

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

发布评论

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

评论(3

書生途 2024-07-20 00:29:47

我怀疑你找错了树。 请记住,用户可能会突然失去互联网连接,浏览器可能崩溃,或者使用红色大开关关闭计算机。 在某些情况下,服务器根本不会再收到浏览器的消息。

做到这一点的最好方法是使用“死人开关”。 既然您说他们每隔几秒就从数据库中提取信息,请利用这个机会存储(在数据库中)您上次从给定客户端收到消息的时间戳。

每隔一分钟左右,在服务器上执行一次查询以查找几分钟内未轮询的客户端,并将用户设置为离线......所有这些都在服务器上。

I suspect you are barking up the wrong tree. Remember, it is possible for the user to suddenly lose their internet connection, their browser could crash, or switch off their computer using the big red switch. There will be cases where the server simply never hears from the browser again.

The best way to do this is with a "dead man's switch." Since you said that they are pulling information from the database every few seconds, use that opportunity to store (in the database) a timestamp for the last time you heard from a given client.

Every minute or so, on the server, do a query to find clients that have not polled for a couple of minutes, and set the user offline... all on the server.

夢归不見 2024-07-20 00:29:47

JavaScript 不可靠,因为我可以通过异常终止来关闭浏览器。

更可靠的方法可能是从浏览器向服务器定期发送“嗨,我还活着”消息,并让服务器在停止接收这些消息时更改状态。

Javascript cannot be reliable, because I can close my browser by abending it.

A more reliable method might be to send periodic "hi I'm still alive" messages from the browser to the server, and have the server change the status when it stops receiving these messages.

小…红帽 2024-07-20 00:29:47

我只能同意乔尔的观点。 没有可靠的方法让您知道 HTTP 代理何时想要终止会话。

I can only agree with Joel here. There is no reliable way for you to know when the HTTP agent wants to terminate the conversation.

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