A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: PhP & detecting disconnects

  1. #1
    Senior Member
    Join Date
    Feb 2006
    Posts
    107

    Question PhP & detecting disconnects

    What is the best way to check and see if a user has closed the browser and or disconnected from your game?

    I am making a flash/php game and I am open to suggestions.

    JS or PhP?

    I see a lot of people asking about this but I dont find much info? Any one help me out or steer me in a direction to find out?!

    Thanks!

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    You can utilize the window.onunload function and maybe do a quick AJAX call out to the server. A more common way is to have your game periodically polling your server and telling it that you're currently active. Once that polling stops... the player has disconnected.

  3. #3
    Programmer
    Join Date
    Aug 2007
    Posts
    173
    hello,

    i assume your making a multiplayer game.
    what method are you using for connectivity?

    sockets?
    polling?
    Freelance: AS2, AS3, PHP, MySQL, JavaScript
    Skype: hunty93

  4. #4
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    I use both of Taco's suggested approaches for my multiplayer games. In my experience, the onUnload event or onBeforeUnload return function is only moderately effective, depending on the browser. It obviously doesn't work if someone is simply disconnected. It also doesn't work on quitting, killing the browser process, or closing a tab if it isn't the active tab in FF. It's a nice way to let the server clean up if possible, but it's by no means a panacea.

    Typically what I do in addition to that is to issue a new hash upon login. I keep a users_sessions db table that records userID, hash and most recent ping time. When the player logs in again, the hash is changed. Then I have Flash player ping the server every so often with the user's ID and the hash it received when it started up. (There's no reason you couldn't make this repeating call with AJAX if you wanted to). On every ping, the server compares the hash sent and updates the most recent ping time. If the hash doesn't match, the client is logged out. I then use a mark-and-sweep technique to flag users whose most recent ping was >N seconds ago, set them disconnected, perform the necessary game logic and flush them from the active database.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center