I have some flash games (.swf files) embedded in my website.
I'm wondering if I could get data (like the final game score) from them, to create a "top players" database?
Is there any way to get this data from the swf object without recompiling the file?
You could send the data ( the score, for example ) to a php file ( or maybe asp ) and store it in a database.
example:
function SendRequest(dataToBeStoredInDB:URLVariables, callback:Function):void
{
var urlReq:URLRequest = new URLRequest("/path/to/phpfile/on/server.php");
urlReq.method = URLRequestMethod.POST;
urlReq.data = dataToBeStoredInDB;
var loader:URLLoader = new URLLoader(urlReq);
loader.addEventListener( Event.COMPLETE, callback_function );
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
}
Related
I've make an application with AIR and ActionScript 3 ( Using the IntelliJ IDE )
At start of the application, it loads some zip file and extracts their files to put them in the local storage. For futur use ( Or an utilisation if the user kills then re-opens the application )
It works correctly without problem the most of the time except that I have, sometimes, with a very low rate of appearence, a crash of the application during the download of those zip files.
The strange thing is these crash can occur directly in the emulator on my computer ( With a message that indicates an error in AIR ).
It is really difficult to check because these crashes may occur, something, like one time on forty launches and seems to never occur when trace() are with the debug mode.
That makes me think it's probably a problem with the speed of the loading, because all trace() slows down the launches and files loading.
Apparently, it's not a memory crash. I've tested and used the same code to load a really high number of same zip together and it doesn't crash.
I can't directly show the code, because it is implanted in a more complex framework.
The concerned code is something near to this :
private static function fileLoad( url:String ):void
{
var loader:URLLoader = new URLLoader();
var header:URLRequestHeader = new URLRequestHeader( "pragma" , "no-cache" );
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener( Event.COMPLETE, complete );
var request:URLRequest = new URLRequest( url );
request.requestHeaders.push( header );
request.method = URLRequestMethod.GET;
loader.load( request );
}
private static function complete( event:Event ):void
{
var file:File;
file = File.applicationStorageDirectory.resolvePath( "ZIP.zip" );
var bytes:ByteArray = event.target.data as ByteArray;
var stream:FileStream = new FileStream();
stream.open( file , FileMode.WRITE );
stream.writeBytes( bytes );
stream.close();
var loadZip:Function = function( event:FZipEvent ):void
{
var zip:FZipFile = event.file;
if ( zip.sizeUncompressed != 0 )
{
var fileData:Object = {};
var file:File = outputFile.resolvePath( zip.filename );
var stream:FileStream = new FileStream();
file.preventBackup = true;
stream.open( file, FileMode.WRITE );
stream.writeBytes( zip.content );
stream.close();
}
};
var inputFile:File = File.applicationStorageDirectory.resolvePath( "ZIP.zip" );
var outputFile:File = File.applicationStorageDirectory.resolvePath( "folderZip" );
var zipFileBytes:ByteArray = event.target.data as ByteArray;
stream = new FileStream();
stream.open( inputFile , FileMode.READ );
stream.readBytes( zipFileBytes );
stream.close();
var zip:FZip = new FZip();
zip.addEventListener( FZipEvent.FILE_LOADED , loadZip );
zip.load( new URLRequest( inputFile.nativePath ) );
}
I don't really expect a total solution but, if you have encountered a similar problem, I'll be thankfull to know what you've done to resolve it.
Thanks a lot
Here I upload recorded sound to server, but I need to add file name and the user name who is uploading the file. But I don't know how I can to post extra name value pairs with a binary data post?
function onClick(e:MouseEvent)
{
var sba:ByteArray = mp3Encoder.mp3Data;
var req:URLRequest = new URLRequest(URL);
req.contentType = 'application/octet-stream';
req.method = URLRequestMethod.POST;
req.data = sba;
var loader:URLLoader = new URLLoader();
loader.addEventListener( ProgressEvent.PROGRESS, progressHandler );
loader.addEventListener( Event.COMPLETE, completeHandler );
loader.load( req );
}
To do something like that you're likely going to have to use a URLRequest header, check out this information here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequestHeader.html
You can use multipart request for sending both types of data (binary and variables). Check out this answer about how to create it in AS3:
Send file from actionscript to servlet
The following code only reads files rather than writing to them. I'm using Flash Player not Air and the code must save the data to an external website so FileReference and FileStream won't work.
var Update:URLRequest = new URLRequest("http://freememegames.com/wp-content/uploads/highscore.txt");
var Score:URLVariables = new URLVariables();
var Load:URLLoader = new URLLoader();
Update.method = URLRequestMethod.POST;
Score.Name = "Jack";
Score.Value = "100";
Update.data = Score;
Load.load(Update);
Load.addEventListener(Event.COMPLETE, Complete);
function Complete(e:Event):void {
scores.text = String(e.target.data);
}
You're going to have to send this data to PHP, and then have PHP write it into your text file. Just so you know though, what you're trying to do is prone to hacking, and you'll want to introduce server-side security to thwart people manipulating scores.
How to upload a File to server in as3? I don't want to browse file by using FileReference.browse(). I tried using URLLoader to get the file and convert to byteArray but as I send it to server using URLVariables it gives IOError=2032. I want update database on the server. The structure would be like
email (string)
sqlite_db (Blob)
I have to send both of these variables inside one request. Any idea!
The upload process is made much easier by using the UploadPostHelper which is a great class to create multipart data forms:
ByteArray byteArray = [YOUR FILE DATA];
var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'http://your.server.com/destination';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data =
UploadPostHelper.getPostData(
'filename.ext',
byteArray,
{
email:"emailParameter",
other:"otherParameter"
} );
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
// create a loader & send the file to the server;
var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load( urlRequest );
And of course you can listen to the complete event and pass back any information from your server. Hope that helps.
I am using Flash runtime (flash player 10). I have a flv encoded bytearray which I need to send to the server( simple php, no FMS or socket servers) and save there. I can use the urlLoader and post everything but then i won't get the progress percentage, so instead I tried saving it with a file reference like this:
var url_ref:URLRequest = new URLRequest("save_vid.php");
url_ref.contentType = "multipart/form-data; boundary="+getBoundary();
url_ref.data = _baFlvEncoder.byteArray;
url_ref.method = URLRequestMethod.POST;
var upfileRef:FileReference = new FileReference();
upfileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
upfileRef.addEventListener(Event.COMPLETE, videoUploadComplete);
upfileRef.upload(url_ref);
But when I try this, I am getting an error:
Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
Any idea how I can do this?
Try this:
var vars :URLVariables = new URLVariables();
vars.bytearray = _baFlvEncoder.byteArray;
var req :URLRequest = new URLRequest("save_vid.php");
req.method = URLRequestMethod.POST;
req.data = vars;
var ldr :URLLoader = new URLLoader();
ldr.addEventListener( Event.COMPLETE, onLoaded );
ldr.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
ldr.addEventListener( ProgressEvent.PROGRESS, onProgress );
ldr.load( req );
function onProgress( e:ProgressEvent ):void
{
trace( "Progress: " + e.bytesLoaded / e.bytesTotal );
}
and in PHP
$bytearray = $_POST['bytearray']