Question

I'm interested in long-polling - can it be implemented via URLLoader in as3? I've got a php backend that periodically checks for updates, and should push them back to the client. I don't want to use sockets (had tons of issues with antiviruses and firewalls in recent project).

So basically:

  1. Client sends post\get request to php script via URLLoader.
  2. php checks for update, if there is any sends them back, if not, sleeps for 500 ms, than checks again.
  3. Client receives update - and repeats from step 1.

I'm guessing the main question is - will URLLoader\or URLRequest close this connection on timeout, and can I configure it somehow, to wait for update, lets say for 1 minute?

Was it helpful?

Solution

Thanks for all of your comments. I've made a test app to check if URLStream can do what I want, and it does. So, PHP code:

<?php
header('Content-Type: application/json; charset=utf-8');

$sleepTime=1*1000000; //every second

$endTime=time()+60*1; //after one minute

while(time()<$endTime)
{
    usleep($sleepTime);
    pushRandom();
}
echo json_encode(array('time'=>time(), 'message'=>'Bye world!'));
flush();
ob_flush();

function pushRandom()
{
    echo json_encode(array('time'=>time(), 'number'=>  rand(1, 10000)));
    flush();
    ob_flush();
}
?>

What it does - echoes a JSON object every second for a minute. After that echoes Bye World, and finished the polling.

And as3 code:

package com.drabuna.as3longpoll
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.net.URLStream;
    import flash.utils.ByteArray;

    public class LongPoll extends EventDispatcher
    {
        private var url:String;
        private var urlRequest:URLRequest;
        private var urlStream:URLStream;

        private var bytesRead:int=0;

        public function LongPoll(serverURL:String)
        {
            url=serverURL;
        }

        public function startPolling():void
        {
            urlRequest=new URLRequest(url);
            urlStream=new URLStream();
            urlStream.addEventListener(ProgressEvent.PROGRESS, onPollData);
            urlStream.addEventListener(Event.COMPLETE, onPollFinished);
            urlStream.addEventListener(IOErrorEvent.IO_ERROR, onPollError);
            urlStream.load(urlRequest);
        }

        public function endPolling():void
        {
            if(urlStream.connected)
            {
                urlStream.close();
            }
        }

        private function onPollData(e:ProgressEvent):void
        {
            if(urlStream.bytesAvailable>bytesRead)
            {
                var byteArray:ByteArray=new ByteArray();
                urlStream.readBytes(byteArray, bytesRead, urlStream.bytesAvailable-bytesRead);

                var tempString:String=byteArray.readUTFBytes(byteArray.bytesAvailable);

                var error:Boolean=false;
                try
                {
                    var jsonObject:Object=JSON.parse(tempString);

                }
                catch(e:Error)
                {
                    error=true;
                }

                if(!error)
                {
                    bytesRead=urlStream.bytesAvailable;
                    var evt:LongPollEvent=new LongPollEvent(LongPollEvent.POLL_DATA);
                    evt.data=jsonObject;
                    this.dispatchEvent(evt);    
                }
            }
        }

        private function onPollFinished(e:Event):void
        {
            var evt:LongPollEvent=new LongPollEvent(LongPollEvent.POLL_FINISHED);
            this.dispatchEvent(evt);
        }

        private function onPollError(e:IOErrorEvent):void
        {
            var evt:LongPollEvent=new LongPollEvent(LongPollEvent.POLL_ERROR);
            evt.data=e;
            this.dispatchEvent(evt);
        }

    }
}

A simple class that reads the stream, and parses it into as3 objects. Everything works fine!)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top