A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: SMS > Database > Flash

Hybrid View

  1. #1
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    To get the gist of it, what you'll need to do is poll a server side script, whether it's PHP, ASP, Python, whatever, to access the database.

    What you'd do is from your Flash app, you'd send out the request with a timestamp based on the last request. Then your script will return only the new data. That way you're not clogging your tubes from always returning the entire chat log with every request since it will be polling frequently.

    As for code, it's hard to just give you a script, but here's an extremely quick example. Completely untested and insecure, but it'll give you an idea:
    PHP Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <results>
    <?php
        header
    ('Content-type: text/xml');
        
        
    $c mysql_connect('localhost''username''*****');
        
    mysql_select_db('mydb'$c);
        
        
    $query "SELECT `message` FROM `messages` WHERE timestamp > {$_GET['timestamp']}";
        
    $result mysql_query($query$c);
        
        while(
    $row mysql_fetch_assoc($result))
        {
            echo 
    '<m>'.$row->message.'</m>';
        }
    ?>
    </results>
    PHP Code:
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.text.TextField;

    var 
    timestamp:int 0;
    var 
    txt:TextField;

    init();

    function 
    init():void
    {
        
    txt = new TextField();
        
    txt.width stage.stageWidth;
        
    txt.height stage.stageHeight;
        
    addChild(txt);
    }

    function 
    grabMessages():void
    {
        var 
    ldr:URLLoader = new URLLoader();
        var 
    req:URLRequest = new URLRequest("getMessages.php");
        var 
    vars:URLVariables = new URLVariables();
        
    vars.timestamp timestamp;
        
    req.data vars;
        
    ldr.addEventListener(Event.COMPLETEupdateMessages);
        
    ldr.load(req);
    }

    function 
    updateMessages($e:Event):void
    {
        var 
    messages:XML = new XML($e.currentTarget.data);
        for(var 
    i:inti<messages.length(); i++) txt.appendText("\n"+messages[i].value());
        
        
    timestamp = new Date().getTime();
        
    setTimeout(grabMessages3000);

    Last edited by MyFriendIsATaco; 11-25-2009 at 10:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center