locked
VWD 2010 run code when user closes page RRS feed

  • Question

  • User693597704 posted

    Hello everyone. I have decided to roll my own chat system using the membership authentication since I had such a hard time searching for an example using VB for over a week,  Anyways  I have most of the functionality working except one small piece(I hope).

    What I am doing is using a database that stores the chat and who is currently in the room.

    My problem is I need to remove that person only from the database when they leave the room. I thought I could do this in the page_unload section, but as I have found out that is the wrong area......I have the code to remove the person already. I also have read the MSDN article on the ASP page life cycle, but I still don't understand where I run the code when a person leaves the web page to remove them from the database?

     

    Sunday, April 20, 2014 4:17 PM

Answers

  • User-760709272 posted

    You can't do this as the web is stateless and there is only communication to the server when the user makes a request.  You'll need to log each user's activity, ie when they post or when they request the latest list of posts, and assume that people who haven't posted or requested within the last, say, 15 minutes, are considered off line.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 20, 2014 4:47 PM
  • User71929859 posted

    But I remember in yahoo games, when somebody left the games room (say by clicking the X in upper right corner), the person also disappeared from the players in room list as well.... How was this accomplished?

    If you need to achieve that sort of thing in asp.net, you can try SignalR OnDisconnected event. My below blog posts shows that

    http://ruchirac.blogspot.co.nz/2013/02/creating-chat-application-in-aspnet.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 26, 2014 2:19 AM

All replies

  • User-760709272 posted

    You can't do this as the web is stateless and there is only communication to the server when the user makes a request.  You'll need to log each user's activity, ie when they post or when they request the latest list of posts, and assume that people who haven't posted or requested within the last, say, 15 minutes, are considered off line.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 20, 2014 4:47 PM
  • User693597704 posted

    Thank you Aidy,

    I will add that functionality as well.

     

    But I remember in yahoo games, when somebody left the games room (say by clicking the X in upper right corner), the person also disappeared from the players in room list as well.... How was this accomplished?

    Sunday, April 20, 2014 4:59 PM
  • User-760709272 posted

    It will have been some client-side flash\java\activex thing that could communicate when it was terminated.

    Sunday, April 20, 2014 5:35 PM
  • User693597704 posted

    hmmm so how could I accomplish this in javascript? can I place javascript code pointing to the procedure i wrote in the code behind?

    currently I was trying to this with codebehind like this

     Dim PlayerRemovingIs As String = getname
            Dim PlayerRemoved As Boolean = False
            ' PLAYER INFO FOR CSV STYLE  USERNAME;RATING;LastActive,
           
            ChatView.PageIndex = ChatView.PageCount - 1
            ChatView.DataBind()
            ThePlayers = ChatView.Rows.Item(3).Cells(1).Text
            ' PLAYER INFO FOR CSV STYLE  USERNAME;RATING;LastActive,
            Dim SplitPlayer As Array
            Dim SplitInnerPlayer As Array
            Temp = ""
            SplitPlayer = Split(ThePlayers, ",")
            For x = 0 To SplitPlayer.Length - 1
                SplitInnerPlayer = Split(SplitPlayer(x), ";")
                If SplitInnerPlayer(0) <> PlayerRemovingIs Then
                    If Temp = "" Then
                        Temp = SplitPlayer(x)
                    Else
                        Temp = Temp & "," & SplitPlayer(x)
                    End If
                End If
            Next
    
            ThePlayers = Temp
    
            SqlDataSource3.InsertCommandType = SqlDataSourceCommandType.Text
            SqlDataSource3.InsertCommand = "INSERT INTO [SomeRoom1Chat] ([ChatFrom],[ChatMessage],[MessageTime],[PlayersInRoom]) VALUES (@ChatFrom,@ChatMessage,@MessageTime,@PlayersInRoom)"
            SqlDataSource3.InsertParameters.Add("ChatFrom", "Zoombyya.com")
            SqlDataSource3.InsertParameters.Add("ChatMessage", getname & " has left the room")
            SqlDataSource3.InsertParameters.Add("MessageTime", Today)
            SqlDataSource3.InsertParameters.Add("PlayersInRoom", ThePlayers)
            SqlDataSource3.Insert()
            ChatView.DataBind()

    Sunday, April 20, 2014 6:23 PM
  • User-760709272 posted

    You can't do the same thing with javascript as activex\java\flash etc all have a process on the client that they execute inside, and processes are told when they are about to be aborted so they can do clean-up code when that happens.  The process that your javascript is running in is the browser, but javascript doesn't have the security rights (or coding ability) to hook into the actual process that it is running inside, so it can't react to events such as the browser closing, it can only react to events it is told about, and the browser closing is not an event your browser passes on to your page.

    Monday, April 21, 2014 6:17 AM
  • User71929859 posted

    But I remember in yahoo games, when somebody left the games room (say by clicking the X in upper right corner), the person also disappeared from the players in room list as well.... How was this accomplished?

    If you need to achieve that sort of thing in asp.net, you can try SignalR OnDisconnected event. My below blog posts shows that

    http://ruchirac.blogspot.co.nz/2013/02/creating-chat-application-in-aspnet.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 26, 2014 2:19 AM