Topic: interaction between chumby and server

new to chumby and flash, so please bear with me.

i am trying to modify the menu code that raffaele sena put up as a template.  specifically, the code that places several menu items on the screen, and when the user selects an item there is a trace command that displays which item was selected.  i'm trying to extend that functionality by communicating that choice to a server.  here's how i modified the onMenuSelect function:

 function onMenuSelect(i:Number) {
    trace("select " + i);
    var loader:LoadVars = new LoadVars();
    if (i == 0)
        loader.load("http://<<server>>/cgi-bin/choice1.py");
    if (i == 1)
        loader.load("http://<<server>>/cgi-bin/choice2.py");
    if (i == 2)
        loader.load("http://<<server>>/cgi-bin/choice3.py");
    if (i == 3)
        loader.load("http://<<server>>/cgi-bin/choice4.py");
    loader.onLoad = function (success) {
        if (success) {
            trace(loader.getBytesLoaded());
        } else {
            trace("FAIL!");
        }
    }
}

on my end, i have a python CGIHTTPServer that i'm running.  i can see when the program is actually sending requests to the server.  the weird behavior i can't explain is that when i launch the program for the first time and select each item, i see requests being sent to the server.  but after each of the four items has been selected once, no more requests get sent to the server.

the functionality i want is for the python scripts on my server to be called once every time that the item was selected.  what am i doing wrong?

thanks!

Re: interaction between chumby and server

It's very likely that the responses from your server are being cached, and so the requests are not reissued.  The default cache time in the player is 5 minutes, unless the HTTP headers specify otherwise.

There are a couple of ways around this:

1) set up your server or your script to send the correct headers to prevent the content from being cached
2) add a "cache buster" to the requests, so the caching code does not consider them the same URL.  For instance:

loader.load("http://<<server>>/cgi-bin/choice2.py?cache_buster="+((new Date()).getTime()));

3 (edited by Ratzfatz 2010-03-27 14:56:57)

Re: interaction between chumby and server

I remember having similar problems. Might be caused by cache. Try this
loader.load("http://<<server>>/cgi-bin/choiceX.py"+"?"+random_number_string)
where random_number_string is a random number string or just a monotonically increasing number.

Moreover, to be save, I would place the loader.onLoad = function () { ...} BEFORE the loader.load("...."); instructions. I think the loader.load("") sends out the request - but what happens if the reply arrives BEFORE the interpreter arrives at the definition of  the .onLoad-hander (which might happen now and then, when e.g. a timer-routine has to be processed) ?
Happy programming!
Edit: Damn, too late!

Re: interaction between chumby and server

duane, thank you so much!  i modified my server-side scripts to include a "Cache-Control: no-cache" in the header, and it solved the problem.  now i can stop banging my head against the wall.

ratzfatz, thanks for the tip.  i will move the function declaration just to be safe.

also, i didn't try it but that cache-buster idea is clever, so thanks again guys.  would my scripts have just worked unmodified with that trick?

Re: interaction between chumby and server

The "cache-buster" trick is really recommended only if you don't have the ability to control the headers.

All it does is prevent the new requests from using the old response, but the responses still get cached, so eventually the cache gets cluttered up with stuff that is never going to be asked for again.  That doesn't hurt anything, but it's highly inefficient since the stuff that probably *should* be cached will be knocked out.  It does waste bandwidth, which would be important for those with metered Internet access.

Chances are your scripts would work unmodified with that trick, unless it was somehow sensitive to having parameters it didn't understand.