Time sync for remote server
Here’s a quick little utility I used in my last project: a simple class that syncs with a time source. It can be used to ensure your application runs on server time rather than the user’s system clock. You sync once with the remote server at the start, and then Flash runs an internal timer to calculate the time elapsed since you last synced. This can prevent multiple server calls everytime you need a time value.
Inside it uses the getTimer() function, which is good at keeping sync and is unaffected by system clock changes, even mid-application. It’s not a particularly difficult piece of code. I made the class a singleton, but you could use it differently if you wanted.
{
import flash.utils.getTimer;
/**
* TimeSync maintains a seperate time value from the system clock.
*/
public class TimeSync
{
private static var _instance:TimeSync;
public static function getInstance():TimeSync
{
if (_instance == null) _instance = new TimeSync();
return _instance;
}
/**
* Internal time-stamp value to sync to.
* Must be set using sync().
*/
protected var _syncStamp:Number;
/**
* Time-stamp for the internal flash getTimer().
*/
protected var _timerStamp:Number;
public function TimeSync() {}
/**
* The time elapsed since the last sync, in millseconds.
*/
public function get delta():Number
{
return (getTimer() - _timerStamp);
}
/**
* The synchronized time value, in milliseconds. Pass this into
* a new Date object to get formatted time: new Date(time);
*/
public function get time():Number
{
return (_syncStamp + delta);
}
/**
* Sync the timer to a new time value.
* @param time Unix time value in milliseconds
*/
public function sync(time:Number):void
{
_syncStamp = time;
_timerStamp = getTimer();
}
}
}
Download: TimeSync.as
To use it just call the sync() method and set the time value you want to sync to. Then everytime you need the synced time value use the .time property to retrieve it. You can also pass the time value into a new Date object to format it.
var timeSync:TimeSync = TimeSync.getInstance();
timeSync.sync(987654321098); // set unix time value, in milliseconds
trace(timeSync.time); // 987654321098
trace(new Date(timeSync.time)); // Thu Apr 19 05:25:21 GMT+0100 2001
Remote sync example
To get it syncing remotely you just need to retrieve a unix time-stamp from a server. A simple bit of php code can do this:
Then use URLLoader in Flash to retrieve the value. Remember to multiply the value to milliseconds! Unix time is usually returned in seconds but Flash works in milliseconds.
{
var request:URLRequest = new URLRequest(“http://webroo.org/time.php”);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loader.load(request);
}
public function loaderCompleteHandler(event:Event):void
{
var time:Number = (event.target as URLLoader).data.time;
TimeSync.getInstance().sync(time * 1000); // Multiply unix time to ms
}
Download: TimeSyncExample.as
Posted: August 11th, 2009 under Actionscript.
Comment from derek
Time September 8, 2009 at 3:02 am
I’m synchronizing multiple flash clients in an app and this is exactly what I was looking for. Thanks!