浏览器关闭时运行服务器端功能

时间:2021-01-16 19:58:59

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.

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

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.

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

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.

到目前为止,我已经查看了window.onbeforeunload与Javascript,设置一个隐藏的表单变量与此事件的函数 - >当然麻烦的是,我仍然要在我的Code-Behind某处做测试这个隐藏的表单变量最终的服务器端数据库查询,有效地将用户设置为脱机。

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

我可能完全不喜欢这个简单的问题!当然,我会欣赏任何完全不同的替代建议。

Thanks

3 个解决方案

#1


15  

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.

每分钟左右,在服务器上,执行查询以查找未轮询几分钟的客户端,并将用户设置为脱机...全部在服务器上。

#2


2  

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

Javascript不可靠,因为我可以通过异常关闭我的浏览器。

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.

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

#3


2  

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.

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

#1


15  

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.

每分钟左右,在服务器上,执行查询以查找未轮询几分钟的客户端,并将用户设置为脱机...全部在服务器上。

#2


2  

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

Javascript不可靠,因为我可以通过异常关闭我的浏览器。

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.

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

#3


2  

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.

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