views:

5795

answers:

3

Hi friends

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.

Please help me related to that and how to use Global.asax in this matter.

+3  A: 

Add a Global.asax file to your website, and in the Session_End event, you remove the user from your HashTable.

protected void Session_End(Object sender, EventArgs e)
{
    // Remove user from HashTable
}
MartinHN
In my hash table contains key, which is also Session["UserID"] contain, This session is used to pass the parameter usrid of Logout(string usrid),in Session_End event, If you have any idea then please help me...
ashish bhatt
I don't understand your question. Sorry.
MartinHN
+2  A: 

You can use global.asax's session end event to remove the unexpectedly disconnected user :

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.

Canavar
If I make change in my Web.config file for Session TimeOut, Is it work?
ashish bhatt
no, you can lower your session timeout, but it effects whole application in an unexpected way. Maybe you can set a programmatic control on your chat application. for example, if a user do not take any action for 5minutes, you can assume him as disconnected..
Canavar
+1  A: 

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.

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.

Guffa
thanks Guffa, If I used onunload event then It will call Leave() but in that main problem is when I refresh the button then it will call Leave(),I handle F5,AlterF4,Right click on body,but How can handle Refresh button clicked in all browser? If any idea then please help me.
ashish bhatt