views:

431

answers:

2

Slight problem i have been pondering on, i have a class that loads a text from a url as a means to sync the time.

The swf file can be on the same page multiple times, but i want it to check to see if one of the swf is sync'ing, if so then wait for it to complete and load its sync'd value (rather then query the url itself), so that they are all sync'd to the same value (of the 1st swf file to load).

I am trying this using Shared Objects, which looks a little like this (snippets):

public function sync():void
    {
        sharedObject = SharedObject.getLocal("synctime", "/");
        trace(sharedObject.data.startOfRequest, sharedObject.data.endTime);
        if ( ((new Date().getTime() - 10000) < sharedObject.data.startOfRequest) && (sharedObject.data.endTime !== undefined))
        {
            loadUTCFromSharedObject();
        } 
        else if ( ((new Date().getTime() - 10000) < sharedObject.data.startOfRequest) && (sharedObject.data.endTime == undefined ) )
        {
            timer.addEventListener(TimerEvent.TIMER, loadUTCFromSharedObject);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, startSync);
            timer.start();
        } else {
            startSync()
        }
    }

    private function startSync(e:Event = null) 
    {
        var syncRequest:URLRequest = new URLRequest(TIMEURL + "?cb=" + Math.random());
        var syncTimeLoader:URLLoader = new URLLoader();

        syncTimeLoader.dataFormat = URLLoaderDataFormat.TEXT;
        syncTimeLoader.addEventListener(Event.COMPLETE, syncTimeComplete );
        syncTimeLoader.addEventListener(IOErrorEvent.IO_ERROR, syncTimeFailed );

        sharedObject.clear();
        startOfRequest = new Date();
        sharedObject.data.startOfRequest = startOfRequest.getTime();
        sharedObject.flush();
        syncTimeLoader.load(syncRequest);
    }
    private function loadUTCFromSharedObject(e:Event = null):void
    {
        var sharedObjectCheck:SharedObject = SharedObject.getLocal("synctime", "/");

        trace (sharedObject.data.endTime, sharedObjectCheck.data.endTime);
        if ( sharedObject.data.endTime !== undefined )
        {
            timer.removeEventListener(TimerEvent.TIMER, loadUTCFromSharedObject);
            timer.removeEventListener(TimerEvent.TIMER_COMPLETE, startSync);
            dispatchEvent( new LoadUTCTimeEvent(LoadUTCTimeEvent.SYNC_COMPLETE, null, null, sharedObject, true));   
            trace(sharedObject.data.UTCOffset);
        }
    }
    private function syncTimeComplete(event:Event):void 
    {           
        var loadedText:URLLoader = URLLoader(event.target);
        dispatchEvent( new LoadUTCTimeEvent(LoadUTCTimeEvent.SYNC_COMPLETE, loadedText.data, startOfRequest, sharedObject, false));

    }

Event Dispatcher:

public function LoadUTCTimeEvent(eventStatus:String, timeString:String = null, startOfRequest:Date = null, sharedObject:SharedObject = null, loadFromSharedObject:Boolean = false):void
    {
        super(eventStatus);
        if (eventStatus == SYNC_COMPLETE) {
            if (loadFromSharedObject == true) {
                time = sharedObject.data.syncTime;
                UTCOffset = sharedObject.data.UTCOffset;
            } 
            else
            {
                //...snip....//
                sharedObject.data.UTCOffset = UTCOffset;
                sharedObject.data.syncTime = time;
                sharedObject.data.endTime = curDate.getTime();
                sharedObject.flush(1000);
            }

        }
    }

The problem i am getting is that the 1st swf creates the sharedobject fine, but the second one reads the endTime as undefined, unless i refresh the page (i.e it doesnt pick up live changes). Is this method not posible, should i try another aproach?

Thanks in advance.

Edit: decided to add another stack to explain a little more:http://stackoverflow.com/questions/2154460/as3-sync-data-between-3-or-more-identical-flash-objects-on-the-same-page

A: 

you can call a javascript function to do the locking if you stuck with this solution

KARASZI István
no something i want to try, want to keep the control in the flash file, without any other outside interation.
Andy
A: 

Okay, use localconnection

Typically, you should not use Local SharedObjects to sync things. The flush() command has never worked for me in the way you are intending it to.

michael
I will give it a go, thanks.
Andy
localconnection didnt work out too well either, had some issues with more then one open connections :(
Andy
If you have that problem, you need to manage multiple open connections with IDs generated according to the timestamp (with a granularity just small enough so you can account for loading time), or implement some kind of tracking system.. so for example, manage an array with the length of how many swfs are in a given group.. increment the corresponding part of the array each time a movie of that its type is loaded. Have only the like-index instances be able to communicate with each other. Alternatively, generate a flashvar tracking ID to each movie on a page. Only swf with the same ID can talk
michael