A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: SMS > Database > Flash

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1

    SMS > Database > Flash

    Hey folks,

    In short, I'm trying to develop a flash app in which I can display text from a continually updated XML database.

    Users send us a text via SMS and it is then added to our database. What I want to do is take each text message from the database and have it automatically display in a very basic Flash interface as soon as it comes in.

    In a similar way to a simple chat relay, I want new messages to appear at the bottom, pushing older messages up and off the screen.

    I'm no expert with Flash or AS so any support/code you have would be muchly appreciated =] And if it's of any relevance, I'm using CS3... AS2 or 3 - whichever works best!

    Cheers, D.

  2. #2
    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