3


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.

Den
  • 601
  • 3
  • 10
  • where's your code? you should set an event listener with `addEventListener`. – vulkanino Feb 13 '12 at 09:38
  • `_loader = new URLLoader(); _loader.dataFormat = URLLoaderDataFormat.BINARY; _loader.addEventListener(Event.COMPLETE,uploadComplete); _loader.addEventListener(ProgressEvent.PROGRESS, uploadProgress );` all is ok, event just isn't dispatching... – Den Feb 13 '12 at 09:49
  • Have you tried disabling your anti-virus software? see http://stackoverflow.com/questions/2930513/monitoring-file-upload-progress-in-actionscript-3 – Creynders Feb 26 '12 at 18:04
  • Anti-virus is not guilty. `Event.COMPLETE` is firing well. – Den Feb 27 '12 at 06:43
  • Did you look at the link I posted? Because there Event.COMPLETE fired well too. It was only the progress event that didn't. – Creynders Feb 29 '12 at 11:21
  • Yes, thank you. But, at first i use `URLLoader`, not `FileReference`. And here the `Event.PROGRESS` is firing only with **recieved** data, not with the sent one. – Den Feb 29 '12 at 11:36

6 Answers6

1

Try to add this, and see if you are getting an error. That might be the problem.

_loader.addEventListener("ioError", ldrError);    

function ldrError(evt:*):void
{
    trace("ERROR");
}

It could be that there is a problem in your php script.

Benny
  • 2,232
  • 4
  • 26
  • 38
ThomasM
  • 2,577
  • 2
  • 22
  • 28
  • At first i'm using `URLLoader` instead of `Loader` to work with binary data, and `URLLoader` don't has `contentLoaderInfo` property. And at last, i got IOError handler, but it isn't tracing anything. So even the `uploadComplete` event is dispatching after the full data is uploaded, but the progress not. I'll put the whole my code if first post shortly. – Den Feb 13 '12 at 10:51
1

Instead of relying on the ProgressEvent firing, try adding an ENTER_FRAME listener and pull the bytesLoaded from the _loader instance. If the ENTER_FRAME works, you could then try replacing it with a repeating TimerEvent on a short delay to reduce the processing load of ENTER_FRAME.

I'm not able to test this right now but this should at least let you know if it's a problem with ProgressEvent not firing or the streaming itself.

DNJohnson
  • 706
  • 5
  • 10
  • Thanks. I've made an `TimerEvent` but there is a problem. `bytesLoaded` returns the amount of **recieved** bytes, but i need to count **sent** bytes. – Den Feb 24 '12 at 12:37
  • In retrospect, that's obviously the expected behaviour. In that case, I'd say you're going to have to go outside the bounds of Flash. I'm not sure what's going on in your PHP script but if the upload is truly streaming and being saved to the server throughout the upload (such as it would with FTP) then inside your TimerEvent handler, I'd poll a second PHP script that calls **filesize()** on the file being streamed and returns it to Flash. – DNJohnson Feb 24 '12 at 13:00
0

The reason your ProgressEvents aren't firing is because they pertain to the download portion of your request (i.e. the response from the server), and not the upload portion. If the server response had been sizable (say, in the form of a large image), you'd certainly see ProgressEvents, but because your script is just returning a small amount of text, it completes very quickly after the upload finishes, before there is any time for the URLLoader to dispatch a ProgressEvent.

Using the FileReference class, as you mentioned, is the only way Flash can introspect on an upload that's underway, but of course it comes with a number of limitations.

My advice would be to set up your URLLoader with a POST payload of a known size, and then store new Date().getTime() right before calling the load method. Once the COMPLETE event is returned, get the current time again, and compare to get an average upload rate based on the size of the payload. Note that this is only approximate because it inevitably incorporates the download portion too, so your calculated speeds will be slightly lower than in reality.

One other workaround would be to monitor the running progress and speed of an upload on the server side, and report this back by having Flash regularly make calls to a separate upload progress script, until the data has all been transferred.

hanenbro
  • 584
  • 5
  • 16
0

Maybe I've found it, don't use the included download simulator, upload/download for real and it will work!

vulkanino
  • 8,858
  • 6
  • 38
  • 67
  • Good call =) But even no result. I think the problem is in that my POST request is sent "solid" to the server, not chunked in parts, so only `Event.COMPLETE` is dispatching. Nothing more. I think i need to use smthing like stream or another way of speed calculating. – Den Feb 13 '12 at 11:42
  • errrm, it was about SOCKET_DATA =). And what for download simulator are you talking about? – Den Feb 13 '12 at 11:56
0

Was curious how fast you were able to test in the download direction and if you encountered any problems with Windows Systems? I seem to be hitting some sort of bottleneck in Windows systems using various browsers where my download maxes out at ~25Mbps. On Mac OS's and Linux systems, I'm hitting line speed of ~100Mbps.

cubicalmonkey
  • 95
  • 1
  • 12
  • The download is fine, getting real speed, almost equal to speedtest.net result. Counting recieved bytes per second. That not the question. – Den Feb 24 '12 at 17:21
0

Sounds to me like the server script has an error in it. Which would return a response fast that would not trigger the progressEvent.
In this case might return something like an error code blah blah blah on line XX.
A case like this would fire the complete event since the client did get a response. (200 OK)


Have you verified the server side script is ok?

The_asMan
  • 6,352
  • 4
  • 21
  • 34
  • Yes, it is ok. And the `complete` event is firing well, but it isn't what i need. – Den Feb 24 '12 at 22:08
  • I'm still not sold that you understand what I am saying. Did you trace the response from the server and verify that the data is what it should be? Don't assume just because you got a complete event that there wasn't a server side issue. – The_asMan Feb 24 '12 at 22:24
  • We are talking about the **upload** part. The download test works well, progress is firing, bytes are counted. But the **upload** part isn't. The `ProgressEvent` is counting incoming bytes, but i need outcoming. Yes, there is a way to get the uploading speed like "delta Time/totalBytes" in `completeEvent`, but i need to count it during the upload process dynamically. – Den Feb 25 '12 at 09:42
  • Once again I am still not sold on if you verified the server response from your upload. You need to look at the server response in the complete event when the upload is finished. – The_asMan Feb 27 '12 at 16:27
  • Let me spell it out for you. You have an error in the upload script on the server. – The_asMan Feb 27 '12 at 16:28
  • In your uploadComplete function just add something like this trace(_loader.contentLoaderInfo.content) or use HTTPFox the fireFox plug in to examine the data that is sent from the server. – The_asMan Feb 29 '12 at 00:47