开发者

AS3: Shared Objects Issue

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.T开发者_运维百科EXT;
        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:(as3) Sync data between 3 or more identical flash objects on the same page


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜