I'm making an speedtest on AS3. And got such a problem.
I'm using URLLoader( )
in my test, and for download test it work very good, but for uploading test i got some troubles. I'm sending some binary data to my php-script, and checking the progress_event
to get bytesLoaded
, to calculate current speed, but the event is not dispatching, only complete_event
appears when thi whole file is sent. So how can I determine the speed by bytes-sending process?
P.S. I can't use fileReference
, cause I'm using my tests one-by-one and can't make users click in filebrowse() dialog.
Thx.
public function startme( ):void {
_startTime = ( new Date( ) ).getTime( );
var req:URLRequest = new URLRequest();
req.url = "http://smart.t3a.ru/speedtest/test.php";
req.contentType = 'application/octet-stream';
req.method = URLRequestMethod.POST;
req.data = Obj;
//req.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
_loader = new URLLoader();
_loader.dataFormat = URLLoaderDataFormat.BINARY;
_loader.addEventListener(Event.COMPLETE,uploadComplete);
_loader.addEventListener(ProgressEvent.PROGRESS, uploadProgress );
_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onstatus);
_loader.addEventListener(IOErrorEvent.IO_ERROR, eIOError);
_loader.load(req);
}
private function onstatus(event:HTTPStatusEvent):void
{
trace(event);
}
private function eIOError(event:IOErrorEvent):void
{
trace(event);
}
private function uploadComplete( e:Event ):void
{
var endTime:Number = ( new Date( ) ).getTime( );
var totalTime:Number = ( endTime - _startTime ) / 1000;
var totalKB:Number = Obj.length * 8 / 1024;
//_speed = totalKB / totalTime;
if (debug)
{
trace( "U total time: " + totalTime + " total KB: " + totalKB + " speed: " + speed + "KBps" );
}
dispatchEvent( e );
}
private function uploadProgress( e:ProgressEvent ):void
{
var endTime:Number = ( new Date( ) ).getTime( );
var totalTime:Number = ( endTime - _startTime ) / 1000;
if (totalTime > 1)
{
var totalKB:Number = e.bytesLoaded * 8 / 1024;
_speed = totalKB / totalTime;
if (debug)
{
trace( "u total time: " + totalTime + " total KB: " + totalKB + " speed: " + speed + "KBps" );
}
dispatchEvent( e );
}
if (totalTime > 7)
{
var ec:Event = new Event(Event.COMPLETE);
_loader.dispatchEvent(ec);
_loader.close();
}
}
UPD My php script:
<?
$postdata = file_get_contents("php://input");
file_put_contents("inp.bin",var_export($postdata,true));
echo "Done";
?>
Yes it's small, but i realy don't need the data, i need to check how fast is it uploaded.
UPD 2
So, after reading all the answers, comments and googling for hours, i probably got the solution, but it is made in some different way, than i wish at the beginning. Anyway TYVM all, i'll post the solution shortly.