运行服务器端功能关闭浏览器 [英] Running server-side function as browser closes

查看:189
本文介绍了运行服务器端功能关闭浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我创建的C#代码隐藏了一个非常简单的聊天室般的ASP.NET页面。当前的用户/聊天信息会显示在位于一个AJAX更新面板内的控件,并使用定时器 - 他们拉离每隔几秒钟一个数据库的信息。



我试图找到一个简单的方法来处理设置用户的状态为脱机,当他们离开自己的浏览器,而不是击中注销按钮。 离线状态,目前只是1字符(Y / N)为IsOnline。



到目前为止,我已经调查使用Javascript window.onbeforeunload,设置一个隐藏的表单对这一事件的函数变量 - >当然麻烦的是,我还是在我的测试这个隐藏表单变量的代码隐藏在某处做最后的服务器端数据库查询,用户离线有效设置

我可能完全obfusticating这可能简单的问题!当然我会很感激的任何完全不同的另一种建议。



感谢


解决方案

我怀疑你是找错了树。请记住,它有可能为用户突然失去他们的互联网连接,浏览器可能会崩溃,或者使用大红色的开关关掉电脑。将有所在服务器根本不会从浏览器重新审理案件。



要做到这一点,最好的办法是用一个死人的开关。既然你说,他们是从数据库中提取信息,每隔几秒钟,利用这个机会来存储(数据库)为您从一个给定的客户端听到的最后时间的时间戳。



每分钟左右,在服务器上,做一个查询发现,没有调查了几分钟的客户端,用户下线......所有的服务器上进行设置。


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

解决方案

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.

这篇关于运行服务器端功能关闭浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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