A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Handle browser closing

  1. #1
    Senior Member
    Join Date
    Sep 2008
    Posts
    100

    Handle browser closing

    I've found things entailing to this in java, but didn't quite understand it. This might or might not be more of a java question but it's more to do with flash in my book.

    I want to be able handle when a browser tab/browser/some other entity running my flash is called to close. And when I say handle I mean handle it within flash.

    The thing is, I have this online flash, and I can easily make a logout button to create a method for the other users to know a person is going to disconnect. But when someone exits the flash through a method outside of flash, I have no way of telling the other users that this person is going to disconnect.

    So I want to make it so that, when they exit the program running the flash, it will send a command to flash first to run a function letting the other users know. My problem is, I don't entirely know how.

    I've seen a way to use java to handle and even delay a browser from closing. And a way to use java to interact with flashes embedded on the page. But I don't understand java very much.

    So... I kinda need some help

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think when you say java, you mean javascript. They are not the same thing.

    You can communicate back and forth from flash to javascript using ExternalInterface. javascript can indeed catch a window close event and do stuff, including call a method in your flash. But you should never rely on this. Your users may have javascript turned off. Or the browser may not wait for the script to complete. Or maybe someone just tripped over the plug and crashed the computer.

  3. #3
    Senior Member
    Join Date
    Sep 2008
    Posts
    100
    Quote Originally Posted by 5TonsOfFlax View Post
    I think when you say java, you mean javascript. They are not the same thing.

    You can communicate back and forth from flash to javascript using ExternalInterface. javascript can indeed catch a window close event and do stuff, including call a method in your flash. But you should never rely on this. Your users may have javascript turned off. Or the browser may not wait for the script to complete. Or maybe someone just tripped over the plug and crashed the computer.
    Well I can think of an alternative sort of. But I'm having a hard time with it.

    I am trying to make each client send a message every 30 seconds to let the other users know hes still connected, if hes unable to send the message in time the other users will handle him as disconnected and remove every instance they have associated to the person.

    I wanted to keep it simple and just handle the 30 second loop in the enterframe loop, but for some reason if the person minimizes the program running the flash, it is unable to send the message.

    I wanted to see if I could do the same thing with the timer class and avoid the problem, and it seems to avoid that problem. But my problem with using the timer class is I don't know how to reference back to the class the timer is running for.

    Basically meaning I'm in a situation where e.target = timer object
    and e.target.parent = object I'm trying to effect. But it is impossible to refer to the parent of the timer class.

    I saw a fix for this involving a custom event dispatcher. But the event it dispatches doesnt seem to be being caught by the event listener I added on the child I'm adding the listener to.
    Last edited by cody112; 11-18-2011 at 07:12 PM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The keepalive pulse is a much better design. Definitely use a Timer for this. The class instance in the function the timer calls should just be "this", since that function is defined as a method of that class, right?
    The timer does not have a parent, since it is not a DisplayObject.

  5. #5
    Senior Member
    Join Date
    Sep 2008
    Posts
    100
    Quote Originally Posted by 5TonsOfFlax View Post
    The class instance in the function the timer calls should just be "this", since that function is defined as a method of that class, right?
    The timer does not have a parent, since it is not a DisplayObject.
    Erm... The class is a movieclip instance in the library.
    Last edited by cody112; 11-18-2011 at 07:53 PM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The class is not a movieclip instance. The library does not contain instances, either.

    You probably mean that you have a MovieClip symbol in your library which you have associated with a class. When you create one of those by new Whatever or by placing it on the display, you create an instance of that class.

    If your function is defined in that class, then within that function the "this" keyword will be bound to that instance of the class.

    If you wrote your function in the frame script, I'm not sure whether it is transformed to a method in which "this" is bound, or if it's a standard Function object in which the meaning of "this" can change. But why would you do it in frame script anyway?

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