-1

How do I write a URLLoader in AS3 that downloads data from the URL, updates the game, then calls itself again, indefinitely. I have already used the following but receive the error: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error.

function getState()
{
    var stateLoader=new URLLoader();
    stateLoader.addEventListener(Event.COMPLETE,loadState);
    stateLoader.load(new URLRequest("http://localhost/mySite/index.php"));
}
function loadState(event:Event)
{
    //game update goes here
    getState();
}
getState();

The code works as desired if I remove the getState(); from inside the loadState function, but obviously doesn't loop.

1 Answers1

0

First of all, if you want your game be in sync with server, consider different backend setup, the one that would allow you to use TCP sockets: they consume less traffic and allow to build another architecture where server is the one responsible for updating client with changes - that kind of setup will be faster and more responsive.

Then, your code. The error means that server is unable to process your request for whatever reason. That aside, spamming HTTP requests is not a good practice as browsers have a certain limit on the amount of outgoing HTTP requests (I'm not sure about total limit, but definitely there's one for the same URL, like about 10, that differs from browser to browser). Thus it is a good idea to insert a small pause between the moment you get an answer from the server and the next time you request an update. That also will relieve your server a GREAT deal.

// Keep the reference to the URLLoader instance
// if you don't want it to be randomly garbage collected.
var stateLoader:URLLoader;

// Clean up the previous request.
function clearLoader():void
{
    if (!stateLoader) return;

    stateLoader.removeEventListener(Event.COMPLETE, loadState);
    stateLoader = null;
}

function getState():void
{
    // Create new instance.
    stateLoader = new URLLoader;

    // Subscribe for COMPLETE event.
    stateLoader.addEventListener(Event.COMPLETE, onState);

    // Obviously, handle I/O errors.
    stateLoader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);

    // Request the update.
    stateLoader.load(new URLRequest("http://localhost/mySite/index.php"));
}

// 
function onError(e:IOErrorEvent):void
{
    // Check if the error is from the current URLLoader instance.
    // If no, then ignore the event.
    if (e.target != stateLoader) return;

    // Remove the URLLoader instance, you don't need this one any more.
    clearLoader();

    // Call next update in 1 second.
    setTimeout(getState, 1000);
}

function onState(e:Event):void
{
    // Check if the error is from the current URLLoader instance.
    // If no, then ignore the event.
    if (e.target != stateLoader) return;

    // Game update goes here.

    // Remove the URLLoader instance, you don't need this one any more.
    clearLoader();

    // Call next update in 0.5 seconds.
    setTimeout(getState, 500);
}

getState();
Organis
  • 6,618
  • 2
  • 9
  • 10