views:

138

answers:

3

I am developing a relay chat application , divided into 2 panes.

right pane - > The Chat responses of users (this uses a ASP.NET Multiline Label control placed inside the update panel , so when any user types the responses and submits it is added to this control)

left panes -> the list of users currently online(this uses ASP.NET list control which is also placed inside the update panel).

below this is the textbox for the user to enter text and a send button to post his response.

everything works fine. But when user closes the browser window instead of clicking the log out button. the list on the left pane is not getting refreshed.

It happens properly , when the user logs out.

IS there any way to knock of the users name from the list if the user closes the browser?(even before his session is expired on the server side).?

sorry i couldn expose the screen shot.

can any one suggest an idea along with a sample code snippet.?

thanks

vijay

+1  A: 

You can use Javascript to detect when the browser has been closed, and then kick-off an AJAX request back to the server notifying that the user left.

bcwood
@bcwood. It fires off everytime a user moves to another page. I don't think they will be able to reliably use this to detect the user is closing the browser from the reading from this forum. Did I miss something?
klabranche
@klabranche but if they navigate away from the current page, the have left the chat application, which might as well be the same as closing their browser.
bcwood
+2  A: 

You can do it the other way around: ping the server from the browser with an ajax call periodically. If no ping received, remove the user.

Zed
I tried that . seems we are polling and that drinks up the effieciency.
vijaysylvester
I think this is the only reliable way. You can make it more efficient if you only ping if the user did not send any messages for a given time. Also for idle users you can dynamically increase the timeout interval after each ping.
Zed
A: 

Session.TimeOut property. When session timed-out then application event Session_End will be fired.

EDIT:

I am agree with my friends commented belows. http://69.10.233.10/KB/aspnet/SessionEndStatePersister.aspx

SUMMARY:However, when using any kind of state management other than InProc (such as StateServer or SqlStateServer), the ASP.NET web application does not fire the Session_End event, and any code in this method will not be executed.

adatapost
The problem with this is that your session timeout is typically fairly long (default is 20 minutes in ASP.NET), so it would take up to 20 minutes for the server to be notified that a user had left. Not acceptable for a "real time" chat application.
bcwood
But he mentioned the Session_End in the global.asax. That fires when the browser is closed OR the session times out.
klabranche
@klabranche, that is incorrect, Session_End does NOT fire when the browser is closed.
bcwood
I agree though that the TimeOut property can be ignored.
klabranche
Wow.... my brain is failing me then. :)
klabranche
Set Session.TimeOut to 1 min. Read that question "how to notify webserver when user closes the browser (in asp.net) ?"
adatapost
One minute is a huge time . i'm looking for something that kicks of from client side to do this.
vijaysylvester
@bcwood - Thanks for the correction. You were right on. My brain failed me yet again. :)
klabranche