C# 如何处理 global.asax 中的会话结束?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/621744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle session end in global.asax?
提问by ashish bhatt
I'm working in chat application, I used HashTable for containing User and Operator as a Key & Object of ChatRoom Class as a value of HashTable. Main problem is that When user or Operator close browser or disconnected without logout then It is automatically logout on the end of the session.
我在聊天应用程序中工作,我使用 HashTable 来包含用户和操作员作为 ChatRoom 类的键和对象作为 HashTable 的值。主要问题是当用户或操作员关闭浏览器或断开连接而不注销时,它会在会话结束时自动注销。
Please help me related to that and how to use Global.asax in this matter.
请帮助我与此相关以及如何在此问题上使用 Global.asax。
回答by MartinHN
Add a Global.asax file to your website, and in the Session_End event, you remove the user from your HashTable.
将 Global.asax 文件添加到您的网站,然后在 Session_End 事件中,从 HashTable 中删除用户。
protected void Session_End(Object sender, EventArgs e)
{
// Remove user from HashTable
}
回答by Canavar
You can use global.asax's session end event to remove the unexpectedly disconnected user :
您可以使用 global.asax 的会话结束事件来删除意外断开连接的用户:
void Session_End(Object sender, EventArgs E) {
// Clean up session resources
}
but beware, session doesn't end when the user closes his browser or his connection lost. It ends when the session timeout reached.
但请注意,当用户关闭浏览器或连接丢失时,会话不会结束。达到会话超时时结束。
回答by Guffa
The Session_End event doesn't fire when the browser is closed, it fires when the server hasn't gotten a request from the user in a specific time persion (by default 20 minutes). That means that if you use Session_End to remove users, they will stay in the chat for 20 minutes after they have closed the browser.
Session_End 事件不会在浏览器关闭时触发,它会在服务器在特定时间段内(默认为 20 分钟)未收到用户请求时触发。这意味着如果您使用 Session_End 删除用户,他们将在关闭浏览器后在聊天中停留 20 分钟。
I suggest that you keep the time of the last request in the user object. That way you can determine how active the user is, and how likely it is that the user has left the chat. You can for example show any user that has not done anything for two minutes as inactive.
我建议您将上次请求的时间保留在用户对象中。通过这种方式,您可以确定用户的活跃程度以及用户离开聊天的可能性。例如,您可以将两分钟内未做任何事情的任何用户显示为不活动。
You can also let the chat application poll the server periodically (if you don't do that already). This would update the last request time in the object and keep the user alive as long as the chat window is open.
您还可以让聊天应用程序定期轮询服务器(如果您还没有这样做的话)。这将更新对象中的最后一个请求时间,并在聊天窗口打开时让用户保持活动状态。
You can use the onunload event in the browser to send a logout request to the server when the user leaves the page. This of course only works if the user still has net connectivity. The onunload event is also triggered when you reload the page, so you would have to keep track of why the event is triggered to use it.
您可以在浏览器中使用 onunload 事件在用户离开页面时向服务器发送注销请求。这当然只有在用户仍然具有网络连接时才有效。当您重新加载页面时也会触发 onunload 事件,因此您必须跟踪触发该事件的原因才能使用它。
回答by yunus
You can use JavaScript which always runs on the client to send a signal to the server - like "I'm here". If next signal does not come, you can call Leave();
. I used AJAX to do this.
您可以使用始终在客户端上运行的 JavaScript 向服务器发送信号 - 例如“我在这里”。如果下一个信号没有来,你可以调用Leave();
。我使用 AJAX 来做到这一点。